-
Notifications
You must be signed in to change notification settings - Fork 0
/
submux.go
49 lines (41 loc) · 1.36 KB
/
submux.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
package httputils
import (
"net/http"
"strings"
)
// SubMux is a wrapper for http.ServeMux that supports a path prefix.
// This allows ServeMux instances to service subpaths.
// Example usage...
// zzz
type SubMux struct {
prefix string
mux *http.ServeMux
}
// NewSubMux constructs a SubMux with the supplied prefix.
func NewSubMux(prefix string) *SubMux {
return NewSubWithMux(prefix, http.NewServeMux())
}
// NewSubWithMux constructs a SubMux with the supplied prefix,
// and existing ServceMux and re-using
func NewSubWithMux(prefix string, mux *http.ServeMux) *SubMux {
return &SubMux{
prefix: "/" + strings.Trim(prefix, "/"),
mux: mux,
}
}
// Handle is a wrapper around http.ServeMux.Handle.
func (sub *SubMux) Handle(pattern string, handler http.Handler) {
sub.mux.Handle(sub.prefix+"/"+strings.TrimLeft(pattern, "/"), handler)
}
// HandleFunc is a wrapper around http.ServeMux.HandleFunc.
func (sub *SubMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
sub.Handle(pattern, http.HandlerFunc(handler))
}
// Handler is a wrapper around http.ServeMux.Handler.
func (sub *SubMux) Handler(r *http.Request) (h http.Handler, pattern string) {
return sub.mux.Handler(r)
}
// ServeHTTP is a wrapper around http.ServeMux.ServeHTTP.
func (sub *SubMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sub.mux.ServeHTTP(w, r)
}