-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
74 lines (59 loc) · 1.97 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package badger
import (
"net/http"
"sync"
)
type middleware func(http.Handler) http.Handler
// Router is responsible for gathering all routing information and to build all
// handler chaining
type Router struct {
basepath string
middlewares []middleware
routes []Route
lock sync.RWMutex
}
// NewRouter returns a pointer to a newly created router
func NewRouter(path string) *Router {
return &Router{path, []middleware{}, []Route{}, sync.RWMutex{}}
}
// Delete creates a new handler for DELETE method in the router
func (r *Router) Delete(path string, handler http.Handler) {
r.Handle("DELETE", path, handler)
}
// Get creates a new handler for GET method in the router
func (r *Router) Get(path string, handler http.Handler) {
r.Handle("GET", path, handler)
}
// Head creates a new handler for HEAD method in the router
func (r *Router) Head(path string, handler http.Handler) {
r.Handle("HEAD", path, handler)
}
// Options creates a new handler for OPTIONS method in the router
func (r *Router) Options(path string, handler http.Handler) {
r.Handle("OPTIONS", path, handler)
}
// Patch creates a new handler for PATCH method in the router
func (r *Router) Patch(path string, handler http.Handler) {
r.Handle("PATCH", path, handler)
}
// Post creates a new handler for POST method in the router
func (r *Router) Post(path string, handler http.Handler) {
r.Handle("POST", path, handler)
}
// Put creates a new handler for PUT method in the router
func (r *Router) Put(path string, handler http.Handler) {
r.Handle("PUT", path, handler)
}
// Handle creates a new handler for the given method in the router
func (r *Router) Handle(method string, path string, handler http.Handler) {
r.lock.Lock()
defer r.lock.Unlock()
route := Route{method, path, handler}
r.routes = append(r.routes, route)
}
// Use creates a new middleware for the given functions
func (r *Router) Use(mw middleware) {
r.lock.Lock()
defer r.lock.Unlock()
r.middlewares = append(r.middlewares, mw)
}