ss/method.go

41 lines
736 B
Go
Raw Normal View History

package bond
import (
"net/http"
2024-01-09 07:40:32 +03:00
"vultras.su/core/bond/methods"
)
// The type implements functionality for multiplexing
// the methods.
type MethodRouter struct {
2024-01-09 07:40:32 +03:00
methodMap map[methods.Method]Handler
}
func Method() *MethodRouter {
ret := &MethodRouter{}
2024-01-09 07:40:32 +03:00
ret.methodMap = make(map[methods.Method]Handler)
return ret
}
func (mr *MethodRouter) Def(
method methods.Method,
handler Handler,
) *MethodRouter {
_, dup := mr.methodMap[method]
if dup {
panic("got the duplicate method")
}
mr.methodMap[method] = handler
return mr
}
func (mr *MethodRouter) ServeHTTP(w ResponseWriter, r *Request) {
handler, ok := mr.methodMap[methods.Method(r.Method)]
if !ok {
http.NotFound(w, r)
return
}
handler.ServeHTTP(w, r)
}