From 7fbec72c109845ebda4ef61d2ca0b69ffe08f964 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 25 Feb 2024 22:20:41 +0500 Subject: [PATCH] feat: better naming Case for the definining function and added the default handlers. --- cmd/test/main.go | 41 ++++++++++++++++++++++++++++++----------- method.go | 11 ++++++++--- path.go | 15 +++++++++++++-- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index 51e3551..78ff752 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -13,28 +13,47 @@ type GetNotesOptions struct { } var root = bond.Root(bond.Path(). -Def( +Case( "", bond.StaticFile("index.html"), -).Def( +).Case( "hello", - bond.Path().Def( + bond.Path().Case( // Using the relative redirect to force us to the en. "", bond.Redirect("en", statuses.Found), - ).Def( + ).Case( "en", bond.Func(func(c *bond.Context) { c.Printf("Hello, World!") }), - ).Def( + ).Case( "ru", bond.Func(func(c *bond.Context) { c.Printf("Привет, Мир!") }), - ), -).Def( + ).Default(bond.Func(func(c *bond.Context){ + c.SetContentType(contents.Plain) + c.Printf( + "AbsPath: %q\n" + + "Prefix: %q\n" + + "Path: %q\n"+ + "Content-Type: %q\n", + c.AbsPath(), + c.PathPrefix(), + c.Path(), + c.ContentType(), + ) + c.Printf("Query:\n") + for k, vs := range c.Query() { + c.Printf("\t%q:\n", k) + for _, v := range vs { + c.Printf("\t\t%q\n", v) + } + } + })), +).Case( "google", bond.Redirect("https://google.com", statuses.Found), -).Def( +).Case( "web", bond.StaticDir("./static"), -).Def( +).Case( "test", bond.Func(func(c *bond.Context) { c.SetContentType(contents.Plain) c.Printf( @@ -55,9 +74,9 @@ Def( } } }), -).Def( +).Case( "get-notes", - bond.Method().Def( + bond.Method().Case( methods.Get, bond.Func(func(c *bond.Context) { opts := GetNotesOptions{} diff --git a/method.go b/method.go index 620b5af..bc4d4b2 100644 --- a/method.go +++ b/method.go @@ -2,7 +2,8 @@ package bond // The type implements method routing. type MethodRouter struct { - methodMap map[ReqMethod]Handler + methodMap map[ReqMethod] Handler + def Handler } // Returns new empty MethodRouter. @@ -13,7 +14,7 @@ func Method() *MethodRouter { } // Define new handler for the specified method. -func (mr *MethodRouter) Def( +func (mr *MethodRouter) Case( method ReqMethod, handler Handler, ) *MethodRouter { @@ -30,7 +31,11 @@ func (mr *MethodRouter) Def( func (mr *MethodRouter) Handle(c *Context) { handler, ok := mr.methodMap[c.Method()] if !ok { - c.NotFound() + if mr.def == nil { + c.NotFound() + } else { + mr.def.Handle(c) + } return } handler.Handle(c) diff --git a/path.go b/path.go index ffdd5c8..34f8f86 100644 --- a/path.go +++ b/path.go @@ -9,6 +9,7 @@ import ( // The type implements path routing for requests. type PathRouter struct { pathMap map[string] Handler + def Handler } // Returns new empty PathRouter. @@ -20,7 +21,7 @@ func Path() *PathRouter { // Define new handler for the specified path. // The defined path must not contain slashes and will panic otherwise. -func (router *PathRouter) Def(pth string, handler Handler) *PathRouter { +func (router *PathRouter) Case(pth string, handler Handler) *PathRouter { _, dup := router.pathMap[pth] if dup { panic(DupDefErr) @@ -55,7 +56,11 @@ func (router *PathRouter) Handle(c *Context) { handler, ok := router.pathMap[name] if !ok { - c.NotFound() + if router.def == nil { + c.NotFound() + } else { + router.def.Handle(c) + } return } @@ -63,3 +68,9 @@ func (router *PathRouter) Handle(c *Context) { handler.Handle(c) } +// The handler to fall to if no match for paths in the router. +func (router *PathRouter) Default(h Handler) *PathRouter { + router.def = h + return router +} +