-
Notifications
You must be signed in to change notification settings - Fork 1
/
chi.go
136 lines (107 loc) · 3.89 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
package hrt
import (
"net/http"
"github.com/go-chi/chi/v5"
)
// Router redefines [chi.Router] to modify all method-routing functions to
// accept an [http.Handler] instead of a [http.HandlerFunc].
type Router interface {
http.Handler
chi.Routes
// Use appends one or more middlewares onto the Router stack.
Use(middlewares ...func(http.Handler) http.Handler)
// With adds inline middlewares for an endpoint handler.
With(middlewares ...func(http.Handler) http.Handler) Router
// Group adds a new inline-Router along the current routing
// path, with a fresh middleware stack for the inline-Router.
Group(fn func(r Router)) Router
// Route mounts a sub-Router along a `pattern“ string.
Route(pattern string, fn func(r Router)) Router
// Mount attaches another http.Handler along ./pattern/*
Mount(pattern string, h http.Handler)
// Handle and HandleFunc adds routes for `pattern` that matches
// all HTTP methods.
Handle(pattern string, h http.Handler)
HandleFunc(pattern string, h http.HandlerFunc)
// Method and MethodFunc adds routes for `pattern` that matches
// the `method` HTTP method.
Method(method, pattern string, h http.Handler)
MethodFunc(method, pattern string, h http.HandlerFunc)
// HTTP-method routing along `pattern`
Connect(pattern string, h http.Handler)
Delete(pattern string, h http.Handler)
Get(pattern string, h http.Handler)
Head(pattern string, h http.Handler)
Options(pattern string, h http.Handler)
Patch(pattern string, h http.Handler)
Post(pattern string, h http.Handler)
Put(pattern string, h http.Handler)
Trace(pattern string, h http.Handler)
// NotFound defines a handler to respond whenever a route could
// not be found.
NotFound(h http.HandlerFunc)
// MethodNotAllowed defines a handler to respond whenever a method is
// not allowed.
MethodNotAllowed(h http.HandlerFunc)
}
// NewRouter creates a [chi.Router] wrapper that turns all method-routing
// functions to take a regular [http.Handler] instead of an [http.HandlerFunc].
// This allows [hrt.Wrap] to function properly. This router also has the given
// opts injected into its context, so there is no need to call [hrt.Use].
func NewRouter(opts Opts) Router {
r := router{chi.NewRouter()}
r.Use(Use(opts))
return r
}
// NewPlainRouter is like [NewRouter] but does not inject any options into the
// context.
func NewPlainRouter() Router {
return router{chi.NewRouter()}
}
// WrapRouter wraps a [chi.Router] to turn all method-routing functions to take
// a regular [http.Handler] instead of an [http.HandlerFunc]. This allows
// [hrt.Wrap] to function properly.
func WrapRouter(r chi.Router) Router {
return router{r}
}
type router struct{ chi.Router }
func (r router) With(middlewares ...func(http.Handler) http.Handler) Router {
return router{r.Router.With(middlewares...)}
}
func (r router) Group(fn func(r Router)) Router {
return router{r.Router.Group(func(r chi.Router) {
fn(router{r})
})}
}
func (r router) Route(pattern string, fn func(r Router)) Router {
return router{r.Router.Route(pattern, func(r chi.Router) {
fn(router{r})
})}
}
func (r router) Connect(pattern string, h http.Handler) {
r.Router.Method("connect", pattern, h)
}
func (r router) Delete(pattern string, h http.Handler) {
r.Router.Method("delete", pattern, h)
}
func (r router) Get(pattern string, h http.Handler) {
r.Router.Method("get", pattern, h)
}
func (r router) Head(pattern string, h http.Handler) {
r.Router.Method("head", pattern, h)
}
func (r router) Options(pattern string, h http.Handler) {
r.Router.Method("options", pattern, h)
}
func (r router) Patch(pattern string, h http.Handler) {
r.Router.Method("patch", pattern, h)
}
func (r router) Post(pattern string, h http.Handler) {
r.Router.Method("post", pattern, h)
}
func (r router) Put(pattern string, h http.Handler) {
r.Router.Method("put", pattern, h)
}
func (r router) Trace(pattern string, h http.Handler) {
r.Router.Method("trace", pattern, h)
}