diff options
Diffstat (limited to 'mux.go')
-rw-r--r-- | mux.go | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -0,0 +1,28 @@ +package mux + +import "net/http" + +type Mux struct { + http.ServeMux + middlewares []Middleware + groups map[string]*Mux +} + +func New() *Mux { + return &Mux{ + ServeMux: http.ServeMux{}, + middlewares: []Middleware{}, + groups: map[string]*Mux{}, + } +} + +func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) { + for path, g := range m.groups { + m.Handle(path, g) + } + h := http.Handler(&m.ServeMux) + for _, mw := range m.middlewares { + h = mw(h) + } + h.ServeHTTP(w, r) +} |