-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
50 lines (41 loc) · 954 Bytes
/
mux.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package tiddlywikid
import (
"net/http"
"path"
)
type Mux struct {
base string
mu *http.ServeMux
}
func NewRootMux() *Mux {
return &Mux{
base: "",
mu: http.NewServeMux(),
}
}
func NewMux(base string) *Mux {
url := path.Clean(path.Join("/", base) + "/")
return &Mux{
base: url,
mu: http.NewServeMux(),
}
}
func (mux *Mux) NewSubMux(pattern string) *Mux {
url := path.Clean(path.Join(mux.base, pattern) + "/")
return &Mux{
base: url,
mu: mux.mu,
}
}
func (mux *Mux) Handle(pattern string, handler http.Handler) {
mux.mu.Handle(mux.base+pattern, handler)
}
func (mux *Mux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
mux.mu.HandleFunc(mux.base+pattern, handler)
}
func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux.mu.ServeHTTP(w, r)
}
func (mux *Mux) StripPrefix(prefix string, h http.Handler) http.Handler {
return http.StripPrefix(mux.base+prefix, h)
}