-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.go
210 lines (181 loc) · 4.68 KB
/
scope.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
package celerity
import (
"fmt"
"net/http"
"runtime/debug"
"strings"
)
// Scope - A group of routes and subgroups used to represent the routing
// structure for the serve.r
type Scope struct {
server *Server
Path RoutePath
Scopes []*Scope
Routes []Route
Middleware []MiddlewareHandler
PreMiddleware []MiddlewareHandler
}
// NewScope - Initializes a new scope
func newScope(path string) *Scope {
return &Scope{
Path: RoutePath(path),
Scopes: []*Scope{},
Routes: []Route{},
}
}
// Scope - Creates a new sub scope
func (s *Scope) Scope(path string) *Scope {
ss := newScope(path)
ss.server = s.server
ss.Middleware = s.Middleware
s.Scopes = append(s.Scopes, ss)
return ss
}
// GET creates a route for a GET method.
func (s *Scope) GET(path string, handler RouteHandler) Route {
return s.Route(GET, path, handler)
}
// POST creates a route for a POST method.
func (s *Scope) POST(path string, handler RouteHandler) Route {
return s.Route(POST, path, handler)
}
// PUT creates a route for a PUT method.
func (s *Scope) PUT(path string, handler RouteHandler) Route {
return s.Route(PUT, path, handler)
}
// PATCH creates a route for a PATCH method.
func (s *Scope) PATCH(path string, handler RouteHandler) Route {
return s.Route(PATCH, path, handler)
}
// DELETE creates a route for a DELETE method.
func (s *Scope) DELETE(path string, handler RouteHandler) Route {
return s.Route(DELETE, path, handler)
}
// Route - Create a new route within the scope
func (s *Scope) Route(method, path string, handler RouteHandler) Route {
r := &BasicRoute{
Path: RoutePath(path),
Method: method,
Handler: handler,
}
s.Routes = append(s.Routes, r)
return r
}
// ServePath serves static files at a filepath
func (s *Scope) ServePath(path, staticpath string) {
r := &LocalPathRoute{
Path: RoutePath(path),
LocalPath: staticpath,
}
s.Routes = append(s.Routes, r)
}
// ServeFile serves static files at a filepath
func (s *Scope) ServeFile(path, localpath string) {
r := &LocalFileRoute{
Path: RoutePath(path),
LocalPath: localpath,
}
s.Routes = append(s.Routes, r)
}
// Use - Use a middleware function
func (s *Scope) Use(mf ...MiddlewareHandler) {
s.Middleware = append(s.Middleware, mf...)
}
// Pre - Registers middleware to be executed when the scope is first matched.
// This happens before the router searches for routes.
func (s *Scope) Pre(mf ...MiddlewareHandler) {
s.PreMiddleware = append(s.PreMiddleware, mf...)
}
// Match - Check if the scope can handle the incomming url
func (s *Scope) Match(req *http.Request, path string) bool {
ok, rPath := s.Path.Match(path)
if !ok {
return false
}
for _, r := range s.Routes {
if ok, _ := r.Match(req.Method, rPath); ok {
return true
}
}
for _, ss := range s.Scopes {
if ss.Match(req, rPath) {
return true
}
}
return false
}
func notFoundHandler(c Context) Response {
return NewErrorResponse(http.StatusNotFound, "The requested resource was not found")
}
func (s *Scope) handleWithMiddleware(c Context, middleware []MiddlewareHandler) Response {
ok, rPath := s.Path.Match(c.ScopedPath)
c.ScopedPath = rPath
middleware = append(middleware, s.Middleware...)
if !ok {
var h RouteHandler
h = notFoundHandler
for i := len(s.Middleware); i > 0; i-- {
h = s.Middleware[i-1](h)
}
return h(c)
}
ph := func(c Context) Response {
for _, ss := range s.Scopes {
if ss.Match(c.Request, c.ScopedPath) {
return ss.handleWithMiddleware(c, middleware)
}
}
for _, r := range s.Routes {
if ok, _ := r.Match(c.Request.Method, c.ScopedPath); ok {
c.SetParams(r.RoutePath().GetURLParams(c.ScopedPath))
var h RouteHandler
h = r.Handle
for i := len(s.Middleware); i > 0; i-- {
h = s.Middleware[i-1](h)
}
return h(c)
}
}
var h RouteHandler
h = notFoundHandler
for i := len(s.Middleware); i > 0; i-- {
h = s.Middleware[i-1](h)
}
return h(c)
}
for i := len(s.PreMiddleware) - 1; i >= 0; i-- {
ph = s.PreMiddleware[i](ph)
}
return ph(c)
}
// Handle - Handle an incomming URL
func (s *Scope) Handle(c Context) (res Response) {
defer func() {
if r := recover(); r != nil {
res = c.Fail(fmt.Errorf("%v", r))
if c.Env == DEV {
stack := strings.Split(string(debug.Stack()), "\n")
res.Data = stack
}
}
}()
return s.handleWithMiddleware(c, []MiddlewareHandler{})
}
func fixPath(p string) string {
if p[0] != '/' {
return "/" + p
}
return p
}
// Channel creates a new channel route
func (s *Scope) Channel(name, path string, h ChannelHandler) {
ch := NewChannel(h)
ch.Name = name
s.server.Channels[name] = ch
ch.Open()
r := &ChannelRoute{
Path: RoutePath(path),
Channel: ch,
}
s.Routes = append(s.Routes, r)
}