summaryrefslogblamecommitdiff
path: root/mux.go
blob: 1631d1008f58923f8ee72b303d6870a3e5b83a1c (plain) (tree)



























                                                                 
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)
}