-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
260 lines (217 loc) · 7.37 KB
/
router.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
package router
import (
"context"
"fmt"
"net/http"
"regexp"
"sort"
"strings"
"time"
// Packages
server "github.com/mutablelogic/go-server"
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
fcgi "github.com/mutablelogic/go-server/pkg/httpserver/fcgi"
provider "github.com/mutablelogic/go-server/pkg/provider"
)
////////////////////////////////////////////////////////////////////////////////
// TYPES
type router struct {
// Map host to a request router. If host key is empty, then it's a default router
// where the host is not matched
host map[string]*reqrouter
}
// represents a set of handlers to be considered for a request
type reqhandlers []*route
// Ensure interfaces is implemented
var _ http.Handler = (*router)(nil)
var _ server.Router = (*router)(nil)
var _ Router = (*router)(nil)
///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
// Create a new router from the configuration
func New(c Config) (server.Task, error) {
r := new(router)
r.host = make(map[string]*reqrouter, defaultCap)
// Add services
for key, service := range c.Services {
parts := strings.SplitN(key, pathSep, 2)
if len(parts) == 1 {
// Could be interpreted as a host if there is a dot in it, or else
// we assume it's a path
if strings.Contains(parts[0], hostSep) {
parts = append(parts, pathSep)
} else {
parts, parts[0] = append(parts, parts[0]), ""
}
}
r.addServiceEndpoints(parts[0], parts[1], service.Service, service.Middleware...)
}
// Return success
return r, nil
}
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Implement the http.Handler interface to route requests
func (router *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := WithTime(r.Context(), time.Now())
// Process FastCGI environment - remove the REQUEST_PREFIX from the request path
path := r.URL.Path
if env := fcgi.ProcessEnv(r); len(env) > 0 {
if prefix, exists := env[envRequestPrefix]; exists {
path = strings.TrimPrefix(path, canonicalPrefix(prefix))
}
}
// Match the route (TODO: From cache)
matchedRoute, code := router.Match(canonicalHost(r.Host), r.Method, path)
// TODO: Cache the route if not already cached
// Close the body after return
defer r.Body.Close()
// Switch on the status code
switch code {
case http.StatusPermanentRedirect:
// TODO: Change this to a JSON redirect - currently returns HTML
http.Redirect(w, r, r.URL.Path+pathSep, int(code))
case http.StatusNotFound:
httpresponse.Error(w, code, "not found:", r.URL.Path)
case http.StatusMethodNotAllowed:
httpresponse.Error(w, code, "method not allowed:", r.Method)
case http.StatusOK:
r = r.Clone(WithRoute(ctx, matchedRoute))
r.URL.Path = matchedRoute.request
matchedRoute.route.handler(w, r)
default:
httpresponse.Error(w, http.StatusInternalServerError, "Internal error", fmt.Sprint(code))
}
}
func (router *router) AddHandler(ctx context.Context, path string, handler http.Handler, methods ...string) server.Route {
return router.AddHandlerFunc(ctx, path, handler.ServeHTTP, methods...)
}
func (router *router) AddHandlerFunc(ctx context.Context, path string, handler http.HandlerFunc, methods ...string) server.Route {
// Fix the path
if !strings.HasPrefix(path, pathSep) {
path = pathSep + path
}
// Create a new request router for the host
key := canonicalHost(Host(ctx))
if _, exists := router.host[key]; !exists {
router.host[key] = newReqRouter(key)
}
// Add the handler to the set of requests
return router.host[key].AddHandler(ctx, canonicalPrefix(Prefix(ctx)), path, handler, methods...)
}
func (router *router) AddHandlerRe(ctx context.Context, path *regexp.Regexp, handler http.Handler, methods ...string) server.Route {
return router.AddHandlerFuncRe(ctx, path, handler.ServeHTTP, methods...)
}
func (router *router) AddHandlerFuncRe(ctx context.Context, path *regexp.Regexp, handler http.HandlerFunc, methods ...string) server.Route {
// Create a new request router for the host
key := canonicalHost(Host(ctx))
if _, exists := router.host[key]; !exists {
router.host[key] = newReqRouter(key)
}
// Add the handler to the set of requests
return router.host[key].AddHandlerRe(ctx, canonicalPrefix(Prefix(ctx)), path, handler.ServeHTTP, methods...)
}
// Match handlers for a given method, host and path, Returns the match
// and the status code, which can be 200, 308, 404 or 405. If the
// status code is 308, then the path is the redirect path.
func (router *router) Match(host, method, path string) (*matchedRoute, int) {
var results reqhandlers
// Check for host and path
host = canonicalHost(host)
for key, r := range router.host {
if key != "" && !strings.HasSuffix(host, key) {
continue
}
if handlers, redirect := r.matchHandlers(path); len(handlers) > 0 {
results = append(results, handlers...)
} else if redirect != "" {
// Bail out to redirect
return NewMatchedRoute(nil, redirect), http.StatusPermanentRedirect
}
}
// Bail out if no results
if len(results) == 0 {
return nil, http.StatusNotFound
}
// Sort results by prefix and path length, with longest first
sort.Sort(results)
// Match method
for _, r := range results {
// Return the first method which matches
if !r.MatchMethod(method) {
continue
}
// Determine the path
path := strings.TrimPrefix(path, r.prefix)
if path == "" || !strings.HasPrefix(path, pathSep) {
path = pathSep + path
}
// Return the route
return NewMatchedRoute(r, path, r.MatchRe(path)...), http.StatusOK
}
// We had a match but not for the method
return nil, http.StatusMethodNotAllowed
}
func (router *router) Scopes() []string {
scopes := make(map[string]bool)
for _, r := range router.host {
for _, h := range r.prefix {
for _, r := range h.handlers {
for _, s := range r.scopes {
scopes[s] = true
}
}
}
}
// Gather all scopes
result := make([]string, 0, len(scopes))
for scope := range scopes {
result = append(result, scope)
}
// Sort alphabetically
sort.Strings(result)
// Return the result
return result
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// Add a set of endpoints to the router with a prefix and middleware
func (router *router) addServiceEndpoints(host, prefix string, service server.ServiceEndpoints, middleware ...server.Middleware) {
// Set the context
ctx := WithHostPrefix(context.Background(), canonicalHost(host), prefix)
if len(middleware) > 0 {
ctx = WithMiddleware(ctx, middleware...)
}
if label := service.Label(); label != "" {
ctx = provider.WithLabel(ctx, label)
}
// Call the service to add the endpoints
service.AddEndpoints(ctx, router)
}
// Return prefix from context, always starts with a '/'
// and never ends with a '/'
func canonicalPrefix(prefix string) string {
if prefix == "" {
return pathSep
}
return "/" + strings.Trim(prefix, pathSep)
}
// Return host, always starts with a '.' and never ends with a '.'
// or returns empty string if host is empty
func canonicalHost(host string) string {
if host == "" {
return ""
}
return strings.ToLower(hostSep + strings.Trim(host, hostSep))
}
func (r reqhandlers) Len() int {
return len(r)
}
func (r reqhandlers) Less(i, j int) bool {
leni := len(r[i].prefix) + len(r[i].path)
lenj := len(r[j].prefix) + len(r[j].path)
return leni > lenj
}
func (r reqhandlers) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}