ss/method.go

43 lines
770 B
Go
Raw Normal View History

package bond
// The type implements method routing.
type MethodRouter struct {
methodMap map[ReqMethod] Handler
def 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) Case(
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 {
if mr.def == nil {
c.NotFound()
} else {
mr.def.Handle(c)
}
return
}
handler.Handle(c)
}