forked from jeffotoni/quick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick.go
521 lines (442 loc) · 12.2 KB
/
quick.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package quick
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"io"
"net/http"
"regexp"
"runtime/debug"
"strings"
"time"
"github.com/jeffotoni/quick/internal/concat"
p "github.com/jeffotoni/quick/internal/print"
)
const (
ContentTypeAppJSON = `application/json`
ContentTypeAppXML = `application/xml`
ContentTypeTextXML = `text/xml`
)
type contextKey int
const myContextKey contextKey = 0
type HandleFunc func(*Ctx) error
type Route struct {
//Pattern *regexp.Regexp
Group string
Pattern string
Path string
Params string
Method string
handler http.HandlerFunc
}
type ctxServeHttp struct {
Path string
Params string
Method string
ParamsMap map[string]string
}
type Config struct {
BodyLimit int64
MaxBodySize int64
MaxHeaderBytes int64
RouteCapacity int
MoreRequests int // 0 a 1000
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ReadHeaderTimeout time.Duration
}
var defaultConfig = Config{
BodyLimit: 2 * 1024 * 1024,
MaxBodySize: 2 * 1024 * 1024,
MaxHeaderBytes: 1 * 1024 * 1024,
RouteCapacity: 1000,
MoreRequests: 290, // valor de equilibrio
//ReadTimeout: 10 * time.Second,
//WriteTimeout: 10 * time.Second,
//IdleTimeout: 1 * time.Second,
// ReadHeaderTimeout: time.Duration(3) * time.Second,
}
type Zeroth int
const (
Zero Zeroth = 0
)
type Quick struct {
config Config
Cors bool
groups []Group
handler http.Handler
mux *http.ServeMux
routes []*Route
routeCapacity int
mws2 []any
CorsSet func(http.Handler) http.Handler
CorsOptions map[string]string
}
func New(c ...Config) *Quick {
var config Config
if len(c) > 0 {
config = c[0]
} else {
config = defaultConfig
}
if config.RouteCapacity == 0 {
config.RouteCapacity = 1000
}
return &Quick{
routes: make([]*Route, 0, config.RouteCapacity),
routeCapacity: config.RouteCapacity,
mux: http.NewServeMux(),
handler: http.NewServeMux(),
config: config,
}
}
func (q *Quick) Use(mw any, nf ...string) {
if len(nf) > 0 {
if strings.ToLower(nf[0]) == "cors" {
switch mwc := mw.(type) {
case func(http.Handler) http.Handler:
if strings.ToLower(nf[0]) == "cors" {
q.Cors = true
q.CorsSet = mwc
}
}
}
}
q.mws2 = append(q.mws2, mw)
}
func (q *Quick) Get(pattern string, handlerFunc HandleFunc) {
path, params, partternExist := extractParamsPattern(pattern)
route := Route{
Pattern: partternExist,
Path: path,
Params: params,
handler: extractParamsGet(q, path, params, handlerFunc),
Method: MethodGet,
}
q.appendRoute(&route)
q.mux.HandleFunc(path, route.handler)
}
func (q *Quick) Post(pattern string, handlerFunc HandleFunc) {
_, params, partternExist := extractParamsPattern(pattern)
pathPost := concat.String("post#", pattern)
route := Route{
Pattern: partternExist,
Params: params,
Path: pattern,
handler: extractParamsPost(q, handlerFunc),
Method: MethodPost,
}
q.appendRoute(&route)
q.mux.HandleFunc(pathPost, route.handler)
}
func (q *Quick) Put(pattern string, handlerFunc HandleFunc) {
_, params, partternExist := extractParamsPattern(pattern)
pathPut := concat.String("put#", pattern)
route := Route{
Pattern: partternExist,
Path: pattern,
handler: extractParamsPut(q, handlerFunc),
Method: MethodPut,
Params: params,
}
q.appendRoute(&route)
q.mux.HandleFunc(pathPut, route.handler)
}
func (q *Quick) Delete(pattern string, handlerFunc HandleFunc) {
_, params, partternExist := extractParamsPattern(pattern)
pathDelete := concat.String("delete#", pattern)
route := Route{
Pattern: partternExist,
Path: pattern,
Params: params,
handler: extractParamsDelete(q, handlerFunc),
Method: MethodDelete,
}
q.appendRoute(&route)
q.mux.HandleFunc(pathDelete, route.handler)
}
func extractHeaders(req http.Request) map[string][]string {
headersMap := make(map[string][]string)
for key, values := range req.Header {
headersMap[key] = values
}
return headersMap
}
func extractBind(c *Ctx, v interface{}) (err error) {
var req http.Request = *c.Request
if strings.ToLower(req.Header.Get("Content-Type")) == ContentTypeAppJSON ||
strings.ToLower(req.Header.Get("Content-Type")) == "application/json; charset=utf-8" ||
strings.ToLower(req.Header.Get("Content-Type")) == "application/json;charset=utf-8" {
err = json.NewDecoder(bytes.NewReader(c.bodyByte)).Decode(v)
} else if strings.ToLower(req.Header.Get("Content-Type")) == ContentTypeTextXML ||
strings.ToLower(req.Header.Get("Content-Type")) == ContentTypeAppXML {
err = xml.NewDecoder(bytes.NewReader(c.bodyByte)).Decode(v)
}
return err
}
func extractParamsPattern(pattern string) (path, params, partternExist string) {
path = pattern
index := strings.Index(pattern, ":")
if index > 0 {
path = pattern[:index]
path = strings.TrimSuffix(path, "/")
if index == 1 {
path = "/"
}
params = strings.TrimPrefix(pattern, path)
partternExist = pattern
}
return
}
func extractParamsGet(q *Quick, pathTmp, paramsPath string, handlerFunc HandleFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
v := req.Context().Value(myContextKey)
if v == nil {
http.NotFound(w, req)
return
}
cval := v.(ctxServeHttp)
querys := make(map[string]string)
queryParams := req.URL.Query()
for key, values := range queryParams {
querys[key] = values[0]
}
headersMap := extractHeaders(*req)
c := &Ctx{
Response: w,
Request: req,
Params: cval.ParamsMap,
Query: querys,
//bodyByte: extractBodyBytes(req.Body),
//bodyByte: extractBodyBytes(req.Body),
Headers: headersMap,
MoreRequests: q.config.MoreRequests,
}
execHandleFunc(c, handlerFunc)
}
}
func extractParamsPost(q *Quick, handlerFunc HandleFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
v := req.Context().Value(myContextKey)
if v == nil {
http.NotFound(w, req)
return
}
if req.ContentLength > q.config.MaxBodySize {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
return
}
headersMap := extractHeaders(*req)
c := &Ctx{
Response: w,
Request: req,
bodyByte: extractBodyBytes(req.Body),
Headers: headersMap,
MoreRequests: q.config.MoreRequests,
}
execHandleFunc(c, handlerFunc)
}
}
func extractParamsPut(q *Quick, handlerFunc HandleFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
v := req.Context().Value(myContextKey)
if v == nil {
http.NotFound(w, req)
return
}
if req.ContentLength > q.config.MaxBodySize {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
return
}
headersMap := extractHeaders(*req)
cval := v.(ctxServeHttp)
c := &Ctx{
Response: w,
Request: req,
Headers: headersMap,
bodyByte: extractBodyBytes(req.Body),
Params: cval.ParamsMap,
MoreRequests: q.config.MoreRequests,
}
execHandleFunc(c, handlerFunc)
}
}
func extractParamsDelete(q *Quick, handlerFunc HandleFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
v := req.Context().Value(myContextKey)
if v == nil {
http.NotFound(w, req)
return
}
headersMap := extractHeaders(*req)
cval := v.(ctxServeHttp)
c := &Ctx{
Response: w,
Request: req,
Headers: headersMap,
Params: cval.ParamsMap,
MoreRequests: q.config.MoreRequests,
}
execHandleFunc(c, handlerFunc)
}
}
func execHandleFunc(c *Ctx, handleFunc HandleFunc) {
err := handleFunc(c)
if err != nil {
c.Set("Content-Type", "text/plain; charset=utf-8")
// #nosec G104
c.Status(500).SendString(err.Error())
}
}
func extractBodyBytes(r io.ReadCloser) []byte {
b, err := io.ReadAll(r)
if err != nil {
return nil
}
return b
}
func (q *Quick) mwWrapper(handler http.Handler) http.Handler {
for i := range q.mws2 {
switch mw := q.mws2[i].(type) {
case func(http.Handler) http.Handler:
handler = mw(handler)
case func(http.ResponseWriter, *http.Request, http.Handler):
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mw(w, r, handler)
})
}
}
return handler
}
func (q *Quick) appendRoute(route *Route) {
route.handler = q.mwWrapper(route.handler).ServeHTTP
//q.routes = append(q.routes, *route)
q.routes = append(q.routes, route)
}
func (q *Quick) ServeHTTP(w http.ResponseWriter, req *http.Request) {
for i := 0; i < len(q.routes); i++ {
var requestURI = req.URL.Path
var patternUri = q.routes[i].Pattern
if q.routes[i].Method != req.Method {
continue
}
if len(patternUri) == 0 {
patternUri = q.routes[i].Path
}
paramsMap, isValid := createParamsAndValid(requestURI, patternUri)
if !isValid {
continue
}
var c = ctxServeHttp{Path: requestURI, ParamsMap: paramsMap, Method: q.routes[i].Method}
req = req.WithContext(context.WithValue(req.Context(), myContextKey, c))
q.routes[i].handler(w, req)
return
}
http.NotFound(w, req)
}
// createParamsAndValid: Create params map and check if the request URI and pattern URI are valid
func createParamsAndValid(reqURI, patternURI string) (map[string]string, bool) {
params := make(map[string]string)
var tmpPath string
reqURISplt := strings.Split(reqURI, "/")
patternURISplt := strings.Split(patternURI, "/")
if len(reqURISplt) != len(patternURISplt) {
return nil, false
}
for pttrn := 0; pttrn < len(patternURISplt); pttrn++ { // collecting params
if strings.Contains(patternURISplt[pttrn], ":") {
params[patternURISplt[pttrn][1:]] = reqURISplt[pttrn]
tmpPath = concat.String(tmpPath, "/", reqURISplt[pttrn])
} else if strings.Contains(patternURISplt[pttrn], "{") { // regex support
regexPattern := patternURISplt[pttrn][1:]
regexPattern = regexPattern[:len(regexPattern)-1]
rgx := regexp.MustCompile(regexPattern)
params[patternURISplt[pttrn]] = rgx.FindString(reqURISplt[pttrn])
tmpPath = concat.String(tmpPath, "/", rgx.FindString(reqURISplt[pttrn]))
} else {
tmpPath = concat.String(tmpPath, "/", patternURISplt[pttrn])
}
}
// This tmpPath is to check if request's uri is the same that our pattern
if tmpPath[1:] != reqURI {
return nil, false
}
return params, true
}
func (q *Quick) GetRoute() []*Route {
return q.routes
}
func (q *Quick) Static(staticFolder string) {
// generate route get with a pattern like this: /static/:file
// pattern := concat.String(staticFolder, ":file")
// q.Get(pattern, func(c *Ctx) {
// path, _, _ := extractParamsPattern(pattern)
// file := c.Params["file"]
// filePath := concat.String(".", path, "/", file)
// err := qos.FileExist(filePath)
// if err != nil {
// c.Status(http.StatusForbidden).SendString(err.Error())
// return
// }
// fileBytes, err := os.ReadFile(filePath)
// if err != nil {
// c.Status(http.StatusInternalServerError).SendString(err.Error())
// return
// }
// c.Status(http.StatusOK).SendFile(fileBytes)
// })
}
func (q *Quick) execHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
func (q *Quick) corsHandler() http.Handler {
return q.CorsSet(q)
}
func (q *Quick) httpServer(addr string, handler ...http.Handler) *http.Server {
var server *http.Server
if len(handler) > 0 {
//assume o nosso mux
server = &http.Server{
Addr: addr,
Handler: q.execHandler(handler[0]),
// ReadTimeout:
// WriteTimeout:
// MaxHeaderBytes:
// IdleTimeout:
ReadHeaderTimeout: q.config.ReadHeaderTimeout,
}
} else if q.Cors {
server = &http.Server{
Addr: addr,
Handler: q.corsHandler(),
// ReadTimeout:
// WriteTimeout:
// MaxHeaderBytes:
// IdleTimeout:
ReadHeaderTimeout: q.config.ReadHeaderTimeout,
}
} else {
server = &http.Server{
Addr: addr,
Handler: q,
// ReadTimeout:
// WriteTimeout:
// MaxHeaderBytes:
// IdleTimeout:
ReadHeaderTimeout: q.config.ReadHeaderTimeout,
}
}
return server
}
func (q *Quick) Listen(addr string, handler ...http.Handler) error {
if q.config.MoreRequests > 0 {
debug.SetGCPercent(q.config.MoreRequests)
}
server := q.httpServer(addr, handler...)
p.Stdout("\033[0;33mRun Server Quick:", addr, "\033[0m\n")
return server.ListenAndServe()
}