ss/method.go

37 lines
698 B
Go

package bond
// The type implements method routing.
type MethodRouter struct {
methodMap map[ReqMethod]Handler
}
// Returns new empty MethodRouter.
func Method() *MethodRouter {
ret := &MethodRouter{}
ret.methodMap = make(map[ReqMethod]Handler)
return ret
}
// Define new handler for the specified method.
func (mr *MethodRouter) Def(
method ReqMethod,
handler Handler,
) *MethodRouter {
_, dup := mr.methodMap[method]
if dup {
panic("got the duplicate method")
}
mr.methodMap[method] = handler
return mr
}
// Implementing the Handler.
func (mr *MethodRouter) Handle(c *Context) {
handler, ok := mr.methodMap[c.Method()]
if !ok {
c.NotFound()
return
}
handler.Handle(c)
}