summaryrefslogtreecommitdiff
path: root/mux.go
blob: 1631d1008f58923f8ee72b303d6870a3e5b83a1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
}