generated from mythrnr/template-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_group.go
212 lines (177 loc) · 6.02 KB
/
route_group.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package group
import (
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
)
// Middleware is just type alias of middleware function.
//
// Middleware は単にミドルウェア関数の型の別名.
type Middleware func(httprouter.Handle) httprouter.Handle
// RouteGroup is struct to retain group information.
//
// RouteGroup はグループ情報を保持する構造体.
type RouteGroup struct {
children []*RouteGroup
handlers map[string]httprouter.Handle
middlewares []Middleware
path string
}
// New returns a new initialized RouteGroup.
// Define only base path first, other info define by method chaining.
//
// New は初期化された RouteGroup を返す.
// 初めはパスのみ定義し, 他の情報はメソッドチェーンで定義していく.
func New(path string) *RouteGroup {
return &RouteGroup{
children: make([]*RouteGroup, 0),
handlers: make(map[string]httprouter.Handle),
middlewares: make([]Middleware, 0),
path: path,
}
}
// DELETE is a shortcut for (*RouteGroup).Handle(http.MethodDelete, handle).
func (r *RouteGroup) DELETE(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodDelete, handle)
}
// GET is a shortcut for (*RouteGroup).Handle(http.MethodGet, handle).
func (r *RouteGroup) GET(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodGet, handle)
}
// HEAD is a shortcut for (*RouteGroup).Handle(http.MethodHead, handle).
func (r *RouteGroup) HEAD(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodHead, handle)
}
// OPTIONS is a shortcut for (*RouteGroup).Handle(http.MethodOptions, handle).
func (r *RouteGroup) OPTIONS(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodOptions, handle)
}
// PATCH is a shortcut for (*RouteGroup).Handle(http.MethodPatch, handle).
func (r *RouteGroup) PATCH(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodPatch, handle)
}
// POST is a shortcut for (*RouteGroup).Handle(http.MethodPost, handle).
func (r *RouteGroup) POST(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodPost, handle)
}
// PUT is a shortcut for (*RouteGroup).Handle(http.MethodPut, handle).
func (r *RouteGroup) PUT(handle httprouter.Handle) *RouteGroup {
return r.Handle(http.MethodPut, handle)
}
// Any relates its handler to all HTTP methods
// defined in `net/http` and returns itself.
// Handler will be ignored if HTTP method is already registered.
//
// Any はハンドラを `net/http` で定義された HTTP メソッド全てに紐付けて自身を返す.
// 登録済みの HTTP メソッドが指定された場合は無視される.
func (r *RouteGroup) Any(handler httprouter.Handle) *RouteGroup {
return r.Match(
handler,
http.MethodConnect,
http.MethodDelete,
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodPatch,
http.MethodPost,
http.MethodPut,
http.MethodTrace,
)
}
// Children returns self includes specified groups.
//
// Children は指定されたグループを含む自身を返す.
func (r *RouteGroup) Children(children ...*RouteGroup) *RouteGroup {
r.children = append(r.children, children...)
return r
}
// Handle returns self includes specified pair of HTTP method and handler.
// Handler will be overwritten if a registered HTTP method is specified.
//
// Handle は指定された HTTP メソッドとハンドラのペアを含む自身を返す.
// 登録済みの HTTP メソッドが指定された場合, ハンドラは上書きされる.
func (r *RouteGroup) Handle(method string, handler httprouter.Handle) *RouteGroup {
r.handlers[method] = handler
return r
}
// Match relates the handler to all specified HTTP methods and returns itself.
// Handler will be ignored if HTTP method is already registered.
//
// Match はハンドラを指定された HTTP メソッド全てに紐付けて自身を返す.
// 登録済みの HTTP メソッドが指定された場合は無視される.
func (r *RouteGroup) Match(
handler httprouter.Handle,
methods ...string,
) *RouteGroup {
for _, m := range methods {
if _, exists := r.handlers[m]; !exists {
r.handlers[m] = handler
}
}
return r
}
// Middleware returns self includes specified middlewares.
//
// Middleware は指定されたミドルウェアを含む自身を返す.
func (r *RouteGroup) Middleware(middlewares ...Middleware) *RouteGroup {
r.middlewares = append(r.middlewares, middlewares...)
return r
}
// Routes returns a one-dimensional representation of
// the handlers registered in the parent and child hierarchies.
// The path and middleware are inherited,
// and the middleware is registered in the order of registration of
// the parent hierarchy, followed by the order of registration of
// the child hierarchies.
//
// Routes は自身と子階層に登録済みのハンドラを一次元化して返す.
// パスとミドルウェアは引き継がれ, ミドルウェアは親階層の登録順,
// 続いて子階層の登録順で実行されるように登録される.
func (r *RouteGroup) Routes() Routes {
return r.routes("", nil)
}
func (r *RouteGroup) len() int {
cnt := len(r.handlers)
for _, rg := range r.children {
cnt += rg.len()
}
return cnt
}
func (r *RouteGroup) routes(
parentPath string,
parentMiddlewares []Middleware,
) Routes {
parentMiddlewares = append(parentMiddlewares, r.middlewares...)
path := joinPath(parentPath, r.path)
routes := make(Routes, 0, r.len())
for m, h := range r.handlers {
routes = append(routes, &Route{
handler: middlewareWith(h, parentMiddlewares...),
method: m,
path: path,
})
}
for _, rg := range r.children {
routes = append(routes, rg.routes(path, parentMiddlewares)...)
}
return routes
}
func joinPath(ps ...string) string {
var buf strings.Builder
for _, p := range ps {
if p := strings.Trim(p, "/"); p != "" {
buf.WriteByte('/')
buf.WriteString(p)
}
}
if 0 < buf.Len() {
return buf.String()
}
return "/"
}
func middlewareWith(h httprouter.Handle, ms ...Middleware) httprouter.Handle {
for i := len(ms) - 1; 0 <= i; i-- {
h = ms[i](h)
}
return h
}