-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
651 lines (558 loc) · 19.5 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/router source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// Package router provides routing implementation for aah framework.
// Routes config file format is similar to HOCON syntax aka typesafe config
// it gets parsed by `aahframework.org/config`.
//
// aah router internally uses customized version of radix tree implementation from
// `github.com/julienschmidt/httprouter` developer by `@julienschmidt`.
package router
import (
"errors"
"fmt"
"path"
"regexp"
"strings"
"aahframework.org/ahttp.v0"
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
"aahframework.org/security.v0"
"aahframework.org/security.v0/scheme"
"aahframework.org/vfs.v0"
)
const (
wildcardSubdomainPrefix = "*."
methodWebSocket = "WS"
autoRouteNameSuffix = "__aah"
)
var (
// HTTPMethodActionMap is default Controller Action name for corresponding
// HTTP Method. If it's not provided in the route configuration.
HTTPMethodActionMap = map[string]string{
ahttp.MethodGet: "Index",
ahttp.MethodPost: "Create",
ahttp.MethodPut: "Update",
ahttp.MethodPatch: "Update",
ahttp.MethodDelete: "Delete",
ahttp.MethodOptions: "Options",
ahttp.MethodHead: "Head",
ahttp.MethodTrace: "Trace",
}
// ErrNoDomainRoutesConfigFound returned when routes config file not found or doesn't
// have `domains { ... }` config information.
ErrNoDomainRoutesConfigFound = errors.New("router: no domain routes config found")
// ErrRouteConstraintFailed returned when request route constraints failed.
ErrRouteConstraintFailed = errors.New("router: route constraints failed")
)
// aah application interface for minimal purpose
type application interface {
Config() *config.Config
Log() log.Loggerer
VFS() *vfs.VFS
SecurityManager() *security.Manager
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//______________________________________________________________________________
// New method returns the Router instance.
func New(configPath string, appCfg *config.Config) *Router {
return &Router{
configPath: configPath,
aCfg: appCfg,
}
}
// NewWithApp method creates router instance with aah application instance.
func NewWithApp(app interface{}, configPath string) (*Router, error) {
a, ok := app.(application)
if !ok {
return nil, fmt.Errorf("router: not a valid aah application instance")
}
rtr := &Router{configPath: configPath, app: a}
if err := rtr.Load(); err != nil {
return nil, err
}
return rtr, nil
}
// IsDefaultAction method is to identify given action name is defined by
// aah framework in absence of user configured route action name.
func IsDefaultAction(action string) bool {
for _, a := range HTTPMethodActionMap {
if a == action {
return true
}
}
return false
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Router
//______________________________________________________________________________
// Router is used to register all application routes and finds the appropriate
// route information for incoming request path.
type Router struct {
Domains []*Domain
configPath string
rootDomain *Domain
app application
config *config.Config
aCfg *config.Config // kept for backward purpose, to be removed in subsequent release
}
// Load method loads a configuration from given file e.g. `routes.conf` and
// applies env profile override values if available.
func (r *Router) Load() (err error) {
if !r.isExists(r.configPath) {
return fmt.Errorf("router: configuration does not exists: %v", r.configPath)
}
r.config, err = r.readConfig(r.configPath)
if err != nil {
return err
}
// apply aah.conf env variables
if envRoutesValues, found := r.appConfig().GetSubConfig("routes"); found {
log.Debug("env routes {...} values found, applying it")
if err = r.config.Merge(envRoutesValues); err != nil {
return fmt.Errorf("router: routes.conf: %s", err)
}
}
err = r.processRoutesConfig()
return
}
// FindDomain returns domain routes configuration based on http request
// otherwise nil.
func (r *Router) FindDomain(req *ahttp.Request) *Domain {
// DEPRECATED to be removed in v1.0 release
return r.Lookup(req.Host)
}
// Lookup method returns domain for given host otherwise nil.
func (r *Router) Lookup(host string) *Domain {
if len(r.Domains) == 1 {
return r.Domains[0] // only one domain scenario
}
// Extact match of host value
// for e.g.: sample.com:8080, www.sample.com:8080, admin.sample.com:8080
if domain := r.findDomain(host); domain != nil {
return domain
}
// Wildcard match of host value
// for e.g.: router.conf value is `*.sample.com:8080` it matches
// {subdomain}.sample.com
if idx := strings.IndexByte(host, '.'); idx > 0 {
return r.findDomain(wildcardSubdomainPrefix + host[idx+1:])
}
return nil
}
// RootDomain method returns the root domain registered in the routes.conf.
// For e.g.: sample.com, admin.sample.com, *.sample.com.
// Root Domain is `sample.com`.
func (r *Router) RootDomain() *Domain {
return r.rootDomain
}
// DomainAddresses method returns domain addresses (host:port) from
// routes configuration.
func (r *Router) DomainAddresses() []string {
var addresses []string
for _, d := range r.Domains {
addresses = append(addresses, d.Key)
}
return addresses
}
// RegisteredActions method returns all the controller name and it's actions
// configured in the "routes.conf".
func (r *Router) RegisteredActions() map[string]map[string]uint8 {
methods := map[string]map[string]uint8{}
for _, d := range r.Domains {
for _, route := range d.routes {
if route.IsStatic || route.Method == methodWebSocket ||
strings.HasSuffix(route.Name, autoRouteNameSuffix) {
continue
}
addRegisteredAction(methods, route)
}
}
return methods
}
// RegisteredWSActions method returns all the WebSocket name and it's actions
// configured in the "routes.conf".
func (r *Router) RegisteredWSActions() map[string]map[string]uint8 {
methods := map[string]map[string]uint8{}
for _, d := range r.Domains {
for _, route := range d.routes {
if route.Method == methodWebSocket {
addRegisteredAction(methods, route)
}
}
}
return methods
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Router unexpoted methods
//______________________________________________________________________________
func (r *Router) findDomain(key string) *Domain {
key = strings.ToLower(key)
for _, d := range r.Domains {
if d.Key == key {
return d
}
}
return nil
}
func (r *Router) isExists(name string) bool {
if r.app == nil {
return vfs.IsExists(nil, name)
}
return vfs.IsExists(r.app.VFS(), name)
}
func (r *Router) readConfig(name string) (*config.Config, error) {
if r.app == nil {
return config.LoadFile(name)
}
return config.VFSLoadFile(r.app.VFS(), r.configPath)
}
func (r *Router) processRoutesConfig() (err error) {
domains := r.config.KeysByPath("domains")
if len(domains) == 0 {
return ErrNoDomainRoutesConfigFound
}
_ = r.config.SetProfile("domains")
// allocate for no. of domains
r.Domains = make([]*Domain, len(domains))
log.Debugf("Domain count: %d", len(domains))
for idx, key := range domains {
domainCfg, _ := r.config.GetSubConfig(key)
// domain host name
host, found := domainCfg.String("host")
if !found {
err = fmt.Errorf("'%v.host' key is missing", key)
return
}
// Router takes the port-no in the order they found-
// 1) routes.conf `domains.<domain-name>.port`
// 2) aah.conf `server.port`
// 3) 8080
port := strings.TrimSpace(domainCfg.StringDefault("port",
r.appConfig().StringDefault("server.port", "8080")))
if port == "80" || port == "443" {
port = ""
}
domain := &Domain{
Name: domainCfg.StringDefault("name", key),
Host: host,
Port: port,
IsSubDomain: domainCfg.BoolDefault("subdomain", false),
MethodNotAllowed: domainCfg.BoolDefault("method_not_allowed", true),
RedirectTrailingSlash: domainCfg.BoolDefault("redirect_trailing_slash", true),
AutoOptions: domainCfg.BoolDefault("auto_options", true),
DefaultAuth: domainCfg.StringDefault("default_auth", ""),
AntiCSRFEnabled: domainCfg.BoolDefault("anti_csrf_check", true),
CORSEnabled: domainCfg.BoolDefault("cors.enable", false),
trees: make(map[string]*node),
routes: make(map[string]*Route),
}
// Domain Level CORS configuration
if domain.CORSEnabled {
baseCORSCfg, _ := domainCfg.GetSubConfig("cors")
domain.CORS = processBaseCORSSection(baseCORSCfg)
}
// Not Found route support is removed in aah v0.8 release,
// in-favor of Centralized Error Handler.
// Refer to https://docs.aahframework.org/centralized-error-handler.html
// processing static routes
if err = r.processStaticRoutes(domain, domainCfg); err != nil {
return
}
// processing namespace routes
if err = r.processRoutes(domain, domainCfg); err != nil {
return
}
// add domain routes
domain.inferKey()
log.Debugf("Domain: %s, routes found: %d", domain.Key, len(domain.routes))
if log.IsLevelTrace() { // process only if log level is trace
// Static Files routes
log.Trace("Routes: Static")
for _, dr := range domain.routes {
if dr.IsStatic {
log.Trace(dr)
}
}
// Application routes
log.Trace("Routes: Application")
for _, dr := range domain.routes {
if !dr.IsStatic {
log.Trace(dr)
}
}
}
r.Domains[idx] = domain
} // End of domains
// find out root domain
// Note: Assuming of one domain and multiple sub-domains configured
// otherwise it will have first non-subdomain reference.
for _, d := range r.Domains {
if !d.IsSubDomain {
r.rootDomain = d
break
}
}
r.config.ClearProfile()
return
}
func (r *Router) processStaticRoutes(domain *Domain, domainCfg *config.Config) error {
staticCfg, found := domainCfg.GetSubConfig("static")
if !found {
return nil
}
routes, err := parseStaticSection(staticCfg)
if err != nil {
return err
}
for idx := range routes {
if err = domain.AddRoute(routes[idx]); err != nil {
return err
}
}
return nil
}
func (r *Router) processRoutes(domain *Domain, domainCfg *config.Config) error {
routesCfg, found := domainCfg.GetSubConfig("routes")
if !found {
return nil
}
maxBodySizeStr := r.appConfig().StringDefault("request.max_body_size", "5mb")
routes, err := parseSectionRoutes(routesCfg, &parentRouteInfo{
Auth: domain.DefaultAuth,
MaxBodySizeStr: maxBodySizeStr,
CORS: domain.CORS,
AntiCSRFCheck: domain.AntiCSRFEnabled,
CORSEnabled: domain.CORSEnabled,
AuthorizationInfo: &authorizationInfo{Satisfy: "either"},
})
if err != nil {
return err
}
for idx := range routes {
if err = domain.AddRoute(routes[idx]); err != nil {
return err
}
}
// Add form login route per security.conf for configured domains
if r.app != nil && r.app.SecurityManager() != nil {
authSchemes := r.app.SecurityManager().AuthSchemes()
if len(authSchemes) > 0 {
if routeNames, result := domain.isAuthConfigured(r.app.SecurityManager()); !result {
log.Errorf("Auth schemes are configured in 'security.conf', however "+
"these routes have invaild auth scheme or not configured: %s",
strings.Join(routeNames, ", "))
return fmt.Errorf("routes configuration error in domain '%s', please check the logs", domain.Name)
}
}
for kn, s := range authSchemes {
switch sv := s.(type) {
case *scheme.FormAuth:
maxBodySize, _ := ess.StrToBytes(maxBodySizeStr)
name := kn + "_login_submit" + autoRouteNameSuffix // for e.g.: form_auth_login_submit__aah
if domain.LookupByName(name) == nil { // add only if not exists
_ = domain.AddRoute(&Route{Name: name, Path: sv.LoginSubmitURL,
Method: ahttp.MethodPost, Auth: kn, MaxBodySize: maxBodySize})
}
case *scheme.OAuth2:
_ = domain.AddRoute(&Route{
Name: kn + "_login" + autoRouteNameSuffix,
Path: sv.LoginURL,
Method: ahttp.MethodGet,
Auth: kn,
})
_ = domain.AddRoute(&Route{
Name: kn + "_redirect" + autoRouteNameSuffix,
Path: sv.RedirectURL,
Method: ahttp.MethodGet,
Auth: kn,
})
}
}
}
return nil
}
func (r *Router) appConfig() *config.Config {
if r.aCfg == nil {
return r.app.Config()
}
return r.aCfg
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//______________________________________________________________________________
var payloadSupported = regexp.MustCompile(`(POST|PUT|DELETE)`)
func parseSectionRoutes(cfg *config.Config, routeInfo *parentRouteInfo) (routes []*Route, err error) {
for _, routeName := range cfg.Keys() {
// getting 'path'
routePath, found := cfg.String(routeName + ".path")
if !found && ess.IsStrEmpty(routeInfo.PrefixPath) {
err = fmt.Errorf("'%v.path' key is missing", routeName)
return
}
if found && routePath[0] == '^' {
routePath = addSlashPrefix(routePath[1:])
} else {
routePath = path.Join(routeInfo.PrefixPath, addSlashPrefix(routePath))
}
routePath = path.Clean(strings.TrimSpace(routePath))
// route segment parameter constraints
actualRoutePath, routeConstraints, er := parseRouteConstraints(routeName, routePath)
if er != nil {
err = er
return
}
// getting 'method', default to GET, if method not found
routeMethod := strings.ToUpper(cfg.StringDefault(routeName+".method", ahttp.MethodGet))
// getting 'target' info for e.g.: controller, websocket
routeTarget := cfg.StringDefault(routeName+".controller", cfg.StringDefault(routeName+".websocket", routeInfo.Target))
// getting 'action', if not found it will default to `HTTPMethodActionMap`
// based on `routeMethod`. For multiple HTTP method mapping scenario,
// this is required attribute.
routeAction := cfg.StringDefault(routeName+".action", findActionByHTTPMethod(routeMethod))
notToSkip := true
if cfg.IsExists(routeName + ".routes") {
if ess.IsStrEmpty(routeTarget) || ess.IsStrEmpty(routeAction) {
notToSkip = false
}
}
if notToSkip && ess.IsStrEmpty(routeTarget) {
err = fmt.Errorf("'%v.controller' or '%v.websocket' key is missing", routeName, routeName)
return
}
if notToSkip && ess.IsStrEmpty(routeAction) {
err = fmt.Errorf("'%v.action' key is missing or it seems to be multiple HTTP methods", routeName)
return
}
// getting route authentication scheme name
routeAuth := strings.TrimSpace(cfg.StringDefault(routeName+".auth", routeInfo.Auth))
// getting route max body size, GitHub go-aah/aah#83
routeMaxBodySize, er := ess.StrToBytes(cfg.StringDefault(routeName+".max_body_size", routeInfo.MaxBodySizeStr))
if er != nil {
log.Warnf("'%v.max_body_size' value is not a valid size unit, fallback to global limit", routeName)
}
if !payloadSupported.MatchString(routeMethod) {
routeMaxBodySize = 0
}
// getting Anti-CSRF check value, GitHub go-aah/aah#115
routeAntiCSRFCheck := cfg.BoolDefault(routeName+".anti_csrf_check", routeInfo.AntiCSRFCheck)
// Authorization Info
routeAuthorizationInfo, er := parseAuthorizationInfo(cfg, routeName, routeInfo)
if er != nil {
err = er
return
}
// CORS
var cors *CORS
if routeInfo.CORSEnabled && routeMethod != methodWebSocket {
if corsCfg, found := cfg.GetSubConfig(routeName + ".cors"); found {
if corsCfg.BoolDefault("enable", true) {
cors = processCORSSection(corsCfg, routeInfo.CORS)
}
} else {
cors = routeInfo.CORS
}
}
// 'anti_csrf_check', 'cors' and 'max_body_size' not applicable for WebSocket
if routeMethod == methodWebSocket {
routeAntiCSRFCheck = false
cors = nil
routeMaxBodySize = 0
}
if notToSkip {
for _, m := range strings.Split(routeMethod, ",") {
routes = append(routes, &Route{
Name: routeName,
Path: actualRoutePath,
Method: strings.TrimSpace(m),
Target: routeTarget,
Action: routeAction,
ParentName: routeInfo.ParentName,
Auth: routeAuth,
MaxBodySize: routeMaxBodySize,
IsAntiCSRFCheck: routeAntiCSRFCheck,
CORS: cors,
Constraints: routeConstraints,
authorizationInfo: routeAuthorizationInfo,
})
}
}
// loading child routes
if childRoutes, found := cfg.GetSubConfig(routeName + ".routes"); found {
croutes, er := parseSectionRoutes(childRoutes, &parentRouteInfo{
ParentName: routeName,
PrefixPath: routePath,
Target: routeTarget,
Auth: routeAuth,
MaxBodySizeStr: routeInfo.MaxBodySizeStr,
AntiCSRFCheck: routeAntiCSRFCheck,
CORS: cors,
CORSEnabled: routeInfo.CORSEnabled,
AuthorizationInfo: routeAuthorizationInfo,
})
if er != nil {
err = er
return
}
routes = append(routes, croutes...)
}
}
return
}
func parseStaticSection(cfg *config.Config) (routes []*Route, err error) {
for _, routeName := range cfg.Keys() {
route := &Route{Name: routeName, Method: ahttp.MethodGet, IsStatic: true}
// getting 'path'
routePath, found := cfg.String(routeName + ".path")
if !found {
err = fmt.Errorf("'static.%v.path' key is missing", routeName)
return
}
// path must begin with '/'
if routePath[0] != slashByte {
err = fmt.Errorf("'static.%v.path' [%v], path must begin with '/'", routeName, routePath)
return
}
if strings.Contains(routePath, ":") || strings.Contains(routePath, "*") {
err = fmt.Errorf("'static.%v.path' parameters can not be used with static", routeName)
return
}
route.Path = path.Clean(routePath)
routeDir, dirFound := cfg.String(routeName + ".dir")
routeFile, fileFound := cfg.String(routeName + ".file")
if dirFound && fileFound {
err = fmt.Errorf("'static.%v.dir' & 'static.%v.file' key(s) cannot be used together", routeName, routeName)
return
}
if !dirFound && !fileFound {
err = fmt.Errorf("either 'static.%v.dir' or 'static.%v.file' key have to be present", routeName, routeName)
return
}
if dirFound {
route.Path = path.Join(route.Path, "*filepath")
}
if fileFound {
// GitHub #141 - for a file mapping
// - 'base_dir' attribute value is not provided and
// - file 'path' value relative path
// then use 'public_assets.dir' as a default value.
if dir, found := cfg.String(routeName + ".base_dir"); found {
routeDir = dir
} else if routeFile[0] != slashByte { // relative file path mapping
if dir, found := cfg.String("public_assets.dir"); found {
routeDir = dir
} else {
err = fmt.Errorf("'static.%v.base_dir' value is missing", routeName)
return
}
}
}
route.Dir = routeDir
route.File = routeFile
route.ListDir = cfg.BoolDefault(routeName+".list", false)
routes = append(routes, route)
}
return
}