feat: better naming Case for the definining function and added the default handlers.

This commit is contained in:
Andrey Parhomenko 2024-02-25 22:20:41 +05:00
parent 6fa4ef4cf4
commit 7fbec72c10
3 changed files with 51 additions and 16 deletions

View file

@ -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{}

View file

@ -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)

15
path.go
View file

@ -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
}