forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chi.go
141 lines (117 loc) · 4.02 KB
/
chi.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package router
import (
"net/http"
"github.com/go-chi/chi"
)
// ChiRouter is an adapter for chi router that implements the Router interface
type ChiRouter struct {
mux chi.Router
}
// NewChiRouterWithOptions creates a new instance of ChiRouter
// with the provided options
func NewChiRouterWithOptions(options Options) *ChiRouter {
router := chi.NewRouter()
router.NotFound(options.NotFoundHandler)
return &ChiRouter{
mux: router,
}
}
// NewChiRouter creates a new instance of ChiRouter
func NewChiRouter() *ChiRouter {
return NewChiRouterWithOptions(DefaultOptions)
}
// ServeHTTP server the HTTP requests
func (r *ChiRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.mux.ServeHTTP(w, req)
}
// Any register a path to all HTTP methods
func (r *ChiRouter) Any(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Handle(path, handler)
}
// Handle registers a path, method and handlers to the router
func (r *ChiRouter) Handle(method string, path string, handler http.HandlerFunc, handlers ...Constructor) {
switch method {
case http.MethodGet:
r.GET(path, handler, handlers...)
case http.MethodPost:
r.POST(path, handler, handlers...)
case http.MethodPut:
r.PUT(path, handler, handlers...)
case http.MethodPatch:
r.PATCH(path, handler, handlers...)
case http.MethodDelete:
r.DELETE(path, handler, handlers...)
case http.MethodHead:
r.HEAD(path, handler, handlers...)
case http.MethodOptions:
r.OPTIONS(path, handler, handlers...)
}
}
// GET registers a HTTP GET path
func (r *ChiRouter) GET(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Get(path, handler)
}
// POST registers a HTTP POST path
func (r *ChiRouter) POST(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Post(path, handler)
}
// PUT registers a HTTP PUT path
func (r *ChiRouter) PUT(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Put(path, handler)
}
// DELETE registers a HTTP DELETE path
func (r *ChiRouter) DELETE(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Delete(path, handler)
}
// PATCH registers a HTTP PATCH path
func (r *ChiRouter) PATCH(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Patch(path, handler)
}
// HEAD registers a HTTP HEAD path
func (r *ChiRouter) HEAD(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Head(path, handler)
}
// OPTIONS registers a HTTP OPTIONS path
func (r *ChiRouter) OPTIONS(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Options(path, handler)
}
// TRACE registers a HTTP TRACE path
func (r *ChiRouter) TRACE(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Trace(path, handler)
}
// CONNECT registers a HTTP CONNECT path
func (r *ChiRouter) CONNECT(path string, handler http.HandlerFunc, handlers ...Constructor) {
r.with(handlers...).Connect(path, handler)
}
// Group creates a child router for a specific path
func (r *ChiRouter) Group(path string) Router {
return &ChiRouter{r.mux.Route(path, nil)}
}
// Use attaches a middleware to the router
func (r *ChiRouter) Use(handlers ...Constructor) Router {
r.mux.Use(r.wrapConstructor(handlers)...)
return r
}
// RoutesCount returns number of routes registered
func (r *ChiRouter) RoutesCount() int {
return r.routesCount(r.mux)
}
func (r *ChiRouter) routesCount(routes chi.Routes) int {
count := len(routes.Routes())
for _, route := range routes.Routes() {
if nil != route.SubRoutes {
count += r.routesCount(route.SubRoutes)
}
}
return count
}
func (r *ChiRouter) with(handlers ...Constructor) chi.Router {
return r.mux.With(r.wrapConstructor(handlers)...)
}
func (r *ChiRouter) wrapConstructor(handlers []Constructor) []func(http.Handler) http.Handler {
var cons = make([]func(http.Handler) http.Handler, 0)
for _, m := range handlers {
cons = append(cons, (func(http.Handler) http.Handler)(m))
}
return cons
}