-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
119 lines (102 loc) · 2.86 KB
/
app.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
package gopeach
import (
"regexp"
"strings"
"github.com/valyala/fasthttp"
)
// match matches path, stores user values and calls handler
//
// e.g. "/users/1" -> "/users/:id" where "id" is stored as key and "1" as value in user values
func match(ctx *RequestCtx, r []Route) func(ctx *RequestCtx) {
for _, route := range r {
ps := strings.Split(string(ctx.Path()), "/")
if ok, err := regexp.Match(route.Path, ctx.Path()); ok && err == nil {
for k, v := range route.ParamIndexes {
ctx.Params[k] = ps[v]
}
return route.Handler
}
}
return func(ctx *RequestCtx) {
ctx.Error("Not Found", fasthttp.StatusNotFound)
}
}
// App stores Routes and middlewares
type App struct {
get []Route
post []Route
patch []Route
put []Route
delete []Route
middleware []func(ctx *RequestCtx)
caseSensitive bool // Default is true
}
// New returns a new App
func New() *App {
return &App{
get: []Route{},
post: []Route{},
patch: []Route{},
put: []Route{},
delete: []Route{},
middleware: []func(ctx *RequestCtx){},
caseSensitive: true,
}
}
// CaseSensitive sets if the routes should be checked with case sensitivity
func (r *App) CaseSensitive(b bool) {
r.caseSensitive = b
}
// Listen listens on addr
func (r *App) Listen(addr string) error {
return fasthttp.ListenAndServe(addr, r.Handler)
}
// ListenTLS listens on addr with certFile and keyFile
func (r *App) ListenTLS(addr, certFile, keyFile string) error {
return fasthttp.ListenAndServeTLS(addr, certFile, keyFile, r.Handler)
}
// Get adds GET Route
func (r *App) Get(s string, h func(ctx *RequestCtx)) {
r.get = append(r.get, NewRoute(s, h))
}
// Post adds POST Route
func (r *App) Post(s string, h func(ctx *RequestCtx)) {
r.post = append(r.post, NewRoute(s, h))
}
// Patch adds PATCH Route
func (r *App) Patch(s string, h func(ctx *RequestCtx)) {
r.patch = append(r.patch, NewRoute(s, h))
}
// Put adds PUT Route
func (r *App) Put(s string, h func(ctx *RequestCtx)) {
r.put = append(r.put, NewRoute(s, h))
}
// Delete adds DELETE Route.
func (r *App) Delete(s string, h func(ctx *RequestCtx)) {
r.delete = append(r.delete, NewRoute(s, h))
}
// Middleware adds middleware
func (r *App) Middleware(h func(ctx *RequestCtx)) {
r.middleware = append(r.middleware, h)
}
// Handler handles requests
func (r *App) Handler(ctx *fasthttp.RequestCtx) {
var h func(ctx *RequestCtx)
req := NewRequestCtx(ctx, r.middleware)
switch string(ctx.Method()) {
case "GET":
h = match(req, r.get)
case "POST":
h = match(req, r.post)
case "PATCH":
h = match(req, r.patch)
case "PUT":
h = match(req, r.put)
case "DELETE":
h = match(req, r.delete)
}
if h != nil {
req.Nexts = append(req.Nexts, h)
}
req.Next()
}