-
Notifications
You must be signed in to change notification settings - Fork 7
/
mountable_router.go
59 lines (48 loc) · 2.03 KB
/
mountable_router.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
51
52
53
54
55
56
57
58
59
package server
import (
"net/http"
libpath "path"
cmp "github.com/snetsystems/cmp/backend"
)
var _ cmp.Router = &MountableRouter{}
// MountableRouter is an implementation of a cmp.Router which supports
// prefixing each route of a Delegated cmp.Router with a prefix.
type MountableRouter struct {
Prefix string
Delegate cmp.Router
}
// DELETE defines a route responding to a DELETE request that will be prefixed
// with the configured route prefix
func (mr *MountableRouter) DELETE(path string, handler http.HandlerFunc) {
mr.Delegate.DELETE(libpath.Join(mr.Prefix, path), handler)
}
// GET defines a route responding to a GET request that will be prefixed
// with the configured route prefix
func (mr *MountableRouter) GET(path string, handler http.HandlerFunc) {
mr.Delegate.GET(libpath.Join(mr.Prefix, path), handler)
}
// POST defines a route responding to a POST request that will be prefixed
// with the configured route prefix
func (mr *MountableRouter) POST(path string, handler http.HandlerFunc) {
mr.Delegate.POST(libpath.Join(mr.Prefix, path), handler)
}
// PUT defines a route responding to a PUT request that will be prefixed
// with the configured route prefix
func (mr *MountableRouter) PUT(path string, handler http.HandlerFunc) {
mr.Delegate.PUT(libpath.Join(mr.Prefix, path), handler)
}
// PATCH defines a route responding to a PATCH request that will be prefixed
// with the configured route prefix
func (mr *MountableRouter) PATCH(path string, handler http.HandlerFunc) {
mr.Delegate.PATCH(libpath.Join(mr.Prefix, path), handler)
}
// Handler defines a prefixed route responding to a request type specified in
// the method parameter
func (mr *MountableRouter) Handler(method string, path string, handler http.Handler) {
mr.Delegate.Handler(method, libpath.Join(mr.Prefix, path), handler)
}
// ServeHTTP is an implementation of http.Handler which delegates to the
// configured Delegate's implementation of http.Handler
func (mr *MountableRouter) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
mr.Delegate.ServeHTTP(rw, r)
}