feat: better naming Case for the definining function and added the default handlers.
This commit is contained in:
parent
6fa4ef4cf4
commit
7fbec72c10
3 changed files with 51 additions and 16 deletions
|
@ -13,28 +13,47 @@ type GetNotesOptions struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var root = bond.Root(bond.Path().
|
var root = bond.Root(bond.Path().
|
||||||
Def(
|
Case(
|
||||||
"",
|
"",
|
||||||
bond.StaticFile("index.html"),
|
bond.StaticFile("index.html"),
|
||||||
).Def(
|
).Case(
|
||||||
"hello",
|
"hello",
|
||||||
bond.Path().Def(
|
bond.Path().Case(
|
||||||
// Using the relative redirect to force us to the en.
|
// Using the relative redirect to force us to the en.
|
||||||
"", bond.Redirect("en", statuses.Found),
|
"", bond.Redirect("en", statuses.Found),
|
||||||
).Def(
|
).Case(
|
||||||
"en", bond.Func(func(c *bond.Context) {
|
"en", bond.Func(func(c *bond.Context) {
|
||||||
c.Printf("Hello, World!")
|
c.Printf("Hello, World!")
|
||||||
}),
|
}),
|
||||||
).Def(
|
).Case(
|
||||||
"ru", bond.Func(func(c *bond.Context) {
|
"ru", bond.Func(func(c *bond.Context) {
|
||||||
c.Printf("Привет, Мир!")
|
c.Printf("Привет, Мир!")
|
||||||
}),
|
}),
|
||||||
),
|
).Default(bond.Func(func(c *bond.Context){
|
||||||
).Def(
|
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),
|
"google", bond.Redirect("https://google.com", statuses.Found),
|
||||||
).Def(
|
).Case(
|
||||||
"web", bond.StaticDir("./static"),
|
"web", bond.StaticDir("./static"),
|
||||||
).Def(
|
).Case(
|
||||||
"test", bond.Func(func(c *bond.Context) {
|
"test", bond.Func(func(c *bond.Context) {
|
||||||
c.SetContentType(contents.Plain)
|
c.SetContentType(contents.Plain)
|
||||||
c.Printf(
|
c.Printf(
|
||||||
|
@ -55,9 +74,9 @@ Def(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
).Def(
|
).Case(
|
||||||
"get-notes",
|
"get-notes",
|
||||||
bond.Method().Def(
|
bond.Method().Case(
|
||||||
methods.Get,
|
methods.Get,
|
||||||
bond.Func(func(c *bond.Context) {
|
bond.Func(func(c *bond.Context) {
|
||||||
opts := GetNotesOptions{}
|
opts := GetNotesOptions{}
|
||||||
|
|
11
method.go
11
method.go
|
@ -2,7 +2,8 @@ package bond
|
||||||
|
|
||||||
// The type implements method routing.
|
// The type implements method routing.
|
||||||
type MethodRouter struct {
|
type MethodRouter struct {
|
||||||
methodMap map[ReqMethod]Handler
|
methodMap map[ReqMethod] Handler
|
||||||
|
def Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns new empty MethodRouter.
|
// Returns new empty MethodRouter.
|
||||||
|
@ -13,7 +14,7 @@ func Method() *MethodRouter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define new handler for the specified method.
|
// Define new handler for the specified method.
|
||||||
func (mr *MethodRouter) Def(
|
func (mr *MethodRouter) Case(
|
||||||
method ReqMethod,
|
method ReqMethod,
|
||||||
handler Handler,
|
handler Handler,
|
||||||
) *MethodRouter {
|
) *MethodRouter {
|
||||||
|
@ -30,7 +31,11 @@ func (mr *MethodRouter) Def(
|
||||||
func (mr *MethodRouter) Handle(c *Context) {
|
func (mr *MethodRouter) Handle(c *Context) {
|
||||||
handler, ok := mr.methodMap[c.Method()]
|
handler, ok := mr.methodMap[c.Method()]
|
||||||
if !ok {
|
if !ok {
|
||||||
c.NotFound()
|
if mr.def == nil {
|
||||||
|
c.NotFound()
|
||||||
|
} else {
|
||||||
|
mr.def.Handle(c)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
handler.Handle(c)
|
handler.Handle(c)
|
||||||
|
|
15
path.go
15
path.go
|
@ -9,6 +9,7 @@ import (
|
||||||
// The type implements path routing for requests.
|
// The type implements path routing for requests.
|
||||||
type PathRouter struct {
|
type PathRouter struct {
|
||||||
pathMap map[string] Handler
|
pathMap map[string] Handler
|
||||||
|
def Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns new empty PathRouter.
|
// Returns new empty PathRouter.
|
||||||
|
@ -20,7 +21,7 @@ func Path() *PathRouter {
|
||||||
|
|
||||||
// Define new handler for the specified path.
|
// Define new handler for the specified path.
|
||||||
// The defined path must not contain slashes and will panic otherwise.
|
// 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]
|
_, dup := router.pathMap[pth]
|
||||||
if dup {
|
if dup {
|
||||||
panic(DupDefErr)
|
panic(DupDefErr)
|
||||||
|
@ -55,7 +56,11 @@ func (router *PathRouter) Handle(c *Context) {
|
||||||
|
|
||||||
handler, ok := router.pathMap[name]
|
handler, ok := router.pathMap[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
c.NotFound()
|
if router.def == nil {
|
||||||
|
c.NotFound()
|
||||||
|
} else {
|
||||||
|
router.def.Handle(c)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,3 +68,9 @@ func (router *PathRouter) Handle(c *Context) {
|
||||||
handler.Handle(c)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue