podman
106 строк · 2.7 Кб
1package denco2
3import (4"net/http"5)
6
7// Mux represents a multiplexer for HTTP request.
8type Mux struct{}9
10// NewMux returns a new Mux.
11func NewMux() *Mux {12return &Mux{}13}
14
15// GET is shorthand of Mux.Handler("GET", path, handler).
16func (m *Mux) GET(path string, handler HandlerFunc) Handler {17return m.Handler("GET", path, handler)18}
19
20// POST is shorthand of Mux.Handler("POST", path, handler).
21func (m *Mux) POST(path string, handler HandlerFunc) Handler {22return m.Handler("POST", path, handler)23}
24
25// PUT is shorthand of Mux.Handler("PUT", path, handler).
26func (m *Mux) PUT(path string, handler HandlerFunc) Handler {27return m.Handler("PUT", path, handler)28}
29
30// HEAD is shorthand of Mux.Handler("HEAD", path, handler).
31func (m *Mux) HEAD(path string, handler HandlerFunc) Handler {32return m.Handler("HEAD", path, handler)33}
34
35// Handler returns a handler for HTTP method.
36func (m *Mux) Handler(method, path string, handler HandlerFunc) Handler {37return Handler{38Method: method,39Path: path,40Func: handler,41}42}
43
44// Build builds a http.Handler.
45func (m *Mux) Build(handlers []Handler) (http.Handler, error) {46recordMap := make(map[string][]Record)47for _, h := range handlers {48recordMap[h.Method] = append(recordMap[h.Method], NewRecord(h.Path, h.Func))49}50mux := newServeMux()51for m, records := range recordMap {52router := New()53if err := router.Build(records); err != nil {54return nil, err55}56mux.routers[m] = router57}58return mux, nil59}
60
61// Handler represents a handler of HTTP request.
62type Handler struct {63// Method is an HTTP method.64Method string65
66// Path is a routing path for handler.67Path string68
69// Func is a function of handler of HTTP request.70Func HandlerFunc
71}
72
73// The HandlerFunc type is aliased to type of handler function.
74type HandlerFunc func(w http.ResponseWriter, r *http.Request, params Params)75
76type serveMux struct {77routers map[string]*Router78}
79
80func newServeMux() *serveMux {81return &serveMux{82routers: make(map[string]*Router),83}84}
85
86// ServeHTTP implements http.Handler interface.
87func (mux *serveMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {88handler, params := mux.handler(r.Method, r.URL.Path)89handler(w, r, params)90}
91
92func (mux *serveMux) handler(method, path string) (HandlerFunc, []Param) {93if router, found := mux.routers[method]; found {94if handler, params, found := router.Lookup(path); found {95return handler.(HandlerFunc), params96}97}98return NotFound, nil99}
100
101// NotFound replies to the request with an HTTP 404 not found error.
102// NotFound is called when unknown HTTP method or a handler not found.
103// If you want to use the your own NotFound handler, please overwrite this variable.
104var NotFound = func(w http.ResponseWriter, r *http.Request, _ Params) {105http.NotFound(w, r)106}
107