12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package ss
- // The type implements method routing.
- type MethodRouter struct {
- methodMap map[ReqMethod] Handler
- def Handler
- }
- // Returns new empty MethodRouter.
- func Method(def Handler) *MethodRouter {
- ret := &MethodRouter{}
- ret.methodMap = make(map[ReqMethod]Handler)
- ret.def = def
- 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)
- }
|