-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathglobalmux.go
79 lines (64 loc) · 1.88 KB
/
globalmux.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package kami
import (
"net/http"
"github.com/dimfeld/httptreemux"
)
var (
routes = newRouter()
enable405 = true
)
func init() {
// set up the default 404/405 handlers
NotFound(nil)
MethodNotAllowed(nil)
}
func newRouter() *httptreemux.TreeMux {
r := httptreemux.New()
r.PathSource = httptreemux.URLPath
r.RedirectBehavior = httptreemux.Redirect307
r.RedirectMethodBehavior = map[string]httptreemux.RedirectBehavior{
"GET": httptreemux.Redirect301,
}
return r
}
// Handler returns an http.Handler serving registered routes.
func Handler() http.Handler {
return routes
}
// Handle registers an arbitrary method handler under the given path.
func Handle(method, path string, handler HandlerType) {
routes.Handle(method, path, bless(wrap(handler)))
}
// Get registers a GET handler under the given path.
func Get(path string, handler HandlerType) {
Handle("GET", path, handler)
}
// Post registers a POST handler under the given path.
func Post(path string, handler HandlerType) {
Handle("POST", path, handler)
}
// Put registers a PUT handler under the given path.
func Put(path string, handler HandlerType) {
Handle("PUT", path, handler)
}
// Patch registers a PATCH handler under the given path.
func Patch(path string, handler HandlerType) {
Handle("PATCH", path, handler)
}
// Head registers a HEAD handler under the given path.
func Head(path string, handler HandlerType) {
Handle("HEAD", path, handler)
}
// Head registers a OPTIONS handler under the given path.
func Options(path string, handler HandlerType) {
Handle("OPTIONS", path, handler)
}
// Delete registers a DELETE handler under the given path.
func Delete(path string, handler HandlerType) {
Handle("DELETE", path, handler)
}
// EnableMethodNotAllowed enables or disables automatic Method Not Allowed handling.
// Note that this is enabled by default.
func EnableMethodNotAllowed(enabled bool) {
enable405 = enabled
}