/
interface.go
314 lines (268 loc) · 7.99 KB
/
interface.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package httpserver
import (
"fmt"
"io"
"net/http"
"path"
"path/filepath"
"sort"
"strings"
"github.com/labstack/echo"
"github.com/recallsong/go-utils/net/httpx/filesystem"
)
// Router .
type Router interface {
GET(path string, handler interface{}, options ...interface{})
POST(path string, handler interface{}, options ...interface{})
DELETE(path string, handler interface{}, options ...interface{})
PUT(path string, handler interface{}, options ...interface{})
PATCH(path string, handler interface{}, options ...interface{})
HEAD(path string, handler interface{}, options ...interface{})
CONNECT(path string, handler interface{}, options ...interface{})
OPTIONS(path string, handler interface{}, options ...interface{})
TRACE(path string, handler interface{}, options ...interface{})
Any(path string, handler interface{}, options ...interface{})
Static(prefix, root string, options ...interface{})
File(path, filepath string, options ...interface{})
Add(method, path string, handler interface{}, options ...interface{})
}
// Context handler context.
type Context interface {
SetAttribute(key string, val interface{})
Attribute(key string) interface{}
Attributes() map[string]interface{}
Request() *http.Request
ResponseWriter() http.ResponseWriter
Param(name string) string
ParamNames() []string
}
// Interceptor .
type Interceptor func(handler func(ctx Context) error) func(ctx Context) error
type context struct {
echo.Context
data map[string]interface{}
}
func (c context) SetAttribute(key string, val interface{}) {
if c.data == nil {
c.data = make(map[string]interface{})
}
c.data[key] = val
}
func (c context) Attribute(key string) interface{} {
if c.data == nil {
return nil
}
return c.data[key]
}
func (c context) Attributes() map[string]interface{} {
if c.data == nil {
c.data = make(map[string]interface{})
}
return c.data
}
func (c context) ResponseWriter() http.ResponseWriter {
return c.Context.Response()
}
type routeKey struct {
method string
path string
}
type route struct {
*routeKey
group string
hide bool
desc string
}
func (r *route) String() string {
return fmt.Sprintf("[%s] %-7s %s", r.group, r.method, r.path)
}
type router struct {
p *provider
routeMap map[routeKey]*route
routes []*route
group string
desc string
hide bool
interceptors []echo.MiddlewareFunc
}
func (r *router) Server() *echo.Echo {
return r.p.server
}
func (r *router) Add(method, path string, handler interface{}, options ...interface{}) {
method = strings.ToUpper(method)
key := routeKey{
method: method,
path: path,
}
if rt, ok := r.routeMap[key]; ok {
if rt.group != r.group {
panic(fmt.Errorf("httpserver routes [%s %s] conflict between groups (%s, %s)",
key.method, key.path, rt.group, r.group))
} else {
panic(fmt.Errorf("httpserver routes [%s %s] conflict in group %s",
key.method, key.path, rt.group))
}
}
route := &route{
routeKey: &key,
group: r.group,
hide: r.hide,
}
interceptors := getInterceptors(options)
for _, opt := range options {
processOptions(route, opt)
}
r.routeMap[key] = route
r.routes = append(r.routes, route)
if handler != nil {
r.add(method, path, handler, interceptors)
}
}
type option func(r *route)
func processOptions(r *route, opt interface{}) {
if fn, ok := opt.(option); ok {
fn(r)
}
}
// WithDescription for Route, description for this route
func WithDescription(desc string) interface{} {
return option(func(r *route) {
r.desc = desc
})
}
// WithHide for Route, not print this route
func WithHide(hide bool) interface{} {
return option(func(r *route) {
r.hide = hide
})
}
// WithInterceptor for Router
func WithInterceptor(fn func(handler func(ctx Context) error) func(ctx Context) error) interface{} {
return Interceptor(fn)
}
type routesSorter []*route
func (s routesSorter) Len() int {
return len(s)
}
func (s routesSorter) Less(i, j int) bool {
if s[i].group == s[j].group {
if s[i].path == s[j].path {
return s[i].method < s[j].method
}
return s[i].path < s[j].path
}
return s[i].group < s[j].group
}
func (s routesSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (r *router) Normalize() {
r.routes = nil
for _, route := range r.routeMap {
r.routes = append(r.routes, route)
}
sort.Sort(routesSorter(r.routes))
}
func (r *router) GET(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodGet, path, handler, options...)
}
func (r *router) POST(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodPost, path, handler, options...)
}
func (r *router) DELETE(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodDelete, path, handler, options...)
}
func (r *router) PUT(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodPut, path, handler, options...)
}
func (r *router) PATCH(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodPatch, path, handler, options...)
}
func (r *router) HEAD(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodHead, path, handler, options...)
}
func (r *router) CONNECT(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodConnect, path, handler, options...)
}
func (r *router) OPTIONS(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodOptions, path, handler, options...)
}
func (r *router) TRACE(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodTrace, path, handler, options...)
}
func (r *router) Any(path string, handler interface{}, options ...interface{}) {
r.Add(http.MethodConnect, path, handler, options...)
r.Add(http.MethodDelete, path, handler, options...)
r.Add(http.MethodGet, path, handler, options...)
r.Add(http.MethodHead, path, handler, options...)
r.Add(http.MethodOptions, path, handler, options...)
r.Add(http.MethodPatch, path, handler, options...)
r.Add(http.MethodPost, path, handler, options...)
r.Add(http.MethodPut, path, handler, options...)
r.Add(http.MethodTrace, path, handler, options...)
}
// WithFileSystem for Static And File
func WithFileSystem(fs http.FileSystem) interface{} {
return fs
}
type filesystemPath string
// WithFileSystemPath for Static And File
func WithFileSystemPath(root string) interface{} {
return filesystemPath(root)
}
func (r *router) Static(prefix, root string, options ...interface{}) {
var fs http.FileSystem
for _, opt := range options {
if files, ok := opt.(http.FileSystem); ok {
fs = files
} else if path, ok := opt.(filesystemPath); ok {
root = filepath.Join(string(path), root)
}
}
p := filepath.Join(prefix, "/**")
r.Add(http.MethodGet, p, nil, options...)
interceptors := append(r.interceptors, getInterceptors(options)...)
if fs == nil {
r.p.server.Static(prefix, root) // TODO interceptors ...
r.p.server.File(path.Join(prefix, "index.html"), path.Join(root, "index.html"), interceptors...)
} else {
fs := filesystem.New(fs).SetRoot(root).SetRoute(prefix)
handler := fs.Handler
r.p.server.GET(p, func(c echo.Context) error {
handler.ServeHTTP(c.Response(), c.Request())
return nil
}, interceptors...)
}
}
func (r *router) File(path, file string, options ...interface{}) {
var fs http.FileSystem
for _, opt := range options {
if files, ok := opt.(http.FileSystem); ok {
fs = files
} else if path, ok := opt.(filesystemPath); ok {
file = filepath.Join(string(path), file)
}
}
r.Add(http.MethodGet, path, nil, options...)
interceptors := append(r.interceptors, getInterceptors(options)...)
if fs == nil {
r.p.server.File(path, file, interceptors...)
} else {
fs := filesystem.New(fs).SetRoot(file).SetRoute(path)
handler := fs.Handler
r.p.server.GET(path, func(c echo.Context) error {
handler.ServeHTTP(c.Response(), c.Request())
return nil
}, interceptors...)
}
}
// Response .
type Response interface {
Status(Context) int
ReadCloser(Context) io.ReadCloser
Error(Context) error
}
// ResponseGetter .
type ResponseGetter interface {
Response(ctx Context) Response
}