Skip to content

Commit b05f6cc

Browse files
committed
go-aah/aah#156 integrate router lib with vfs
1 parent c5a4bfe commit b05f6cc

File tree

5 files changed

+55
-62
lines changed

5 files changed

+55
-62
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ go:
1717
go_import_path: aahframework.org/router.v0
1818

1919
before_install:
20-
- bash <(curl -s https://aahframework.org/base-before-install) "ahttp config essentials log"
20+
- bash <(curl -s https://aahframework.org/base-before-install) "vfs ahttp forge config essentials log"
2121

2222
install:
2323
- go get -t -v ./...

cors.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ var (
3737
defaultAllowMethods = []string{ahttp.MethodGet, ahttp.MethodHead, ahttp.MethodPost}
3838
)
3939

40-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
40+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
4141
// CORS
42-
//___________________________________
42+
//______________________________________________________________________________
4343

4444
// CORS struct holds Cross-Origin Resource Sharing (CORS) configuration
4545
// values and verification methods for the route.
@@ -185,9 +185,9 @@ func (c CORS) String() string {
185185
return buf.String()
186186
}
187187

188-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
188+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
189189
// Unexported CORS methods
190-
//___________________________________
190+
//______________________________________________________________________________
191191

192192
func (c *CORS) addHeaders(dst []string, src []string) []string {
193193
for _, h := range src {

cors_test.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ package router
66

77
import (
88
"io/ioutil"
9-
"os"
10-
"path/filepath"
119
"testing"
1210

13-
"aahframework.org/config.v0"
1411
"aahframework.org/essentials.v0"
1512
"aahframework.org/log.v0"
1613
"aahframework.org/test.v0/assert"
@@ -19,10 +16,7 @@ import (
1916
func TestRouterCORS1(t *testing.T) {
2017
_ = log.SetLevel("TRACE")
2118
log.SetWriter(ioutil.Discard)
22-
wd, _ := os.Getwd()
23-
appCfg, _ := config.ParseString("")
24-
router := New(filepath.Join(wd, "testdata", "routes-cors-1.conf"), appCfg)
25-
err := router.Load()
19+
router, err := createRouter("routes-cors-1.conf")
2620
assert.FailNowOnError(t, err, "")
2721

2822
domain := router.Domains["localhost:8080"]
@@ -55,10 +49,8 @@ func TestRouterCORS1(t *testing.T) {
5549

5650
func TestRouterCORS2(t *testing.T) {
5751
_ = log.SetLevel("TRACE")
58-
wd, _ := os.Getwd()
59-
appCfg, _ := config.ParseString("")
60-
router := New(filepath.Join(wd, "testdata", "routes-cors-2.conf"), appCfg)
61-
err := router.Load()
52+
log.SetWriter(ioutil.Discard)
53+
router, err := createRouter("routes-cors-2.conf")
6254
assert.FailNowOnError(t, err, "")
6355

6456
domain := router.Domains["localhost:8080"]

router.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"aahframework.org/config.v0"
2121
"aahframework.org/essentials.v0"
2222
"aahframework.org/log.v0"
23+
"aahframework.org/vfs.v0"
2324
)
2425

2526
const (
@@ -46,13 +47,19 @@ var (
4647
ErrNoDomainRoutesConfigFound = errors.New("router: no domain routes config found")
4748
)
4849

49-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
50+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
5051
// Package methods
51-
//___________________________________
52+
//______________________________________________________________________________
5253

5354
// New method returns the Router instance.
5455
func New(configPath string, appCfg *config.Config) *Router {
56+
return NewWithVFS(nil, configPath, appCfg)
57+
}
58+
59+
// NewWithVFS method creates router instance with given VFS instance.
60+
func NewWithVFS(fs *vfs.VFS, configPath string, appCfg *config.Config) *Router {
5561
return &Router{
62+
vfs: fs,
5663
configPath: configPath,
5764
appCfg: appCfg,
5865
}
@@ -69,30 +76,32 @@ func IsDefaultAction(action string) bool {
6976
return false
7077
}
7178

72-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
79+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
7380
// Router
74-
//___________________________________
81+
//______________________________________________________________________________
7582

7683
// Router is used to register all application routes and finds the appropriate
7784
// route information for incoming request path.
7885
type Router struct {
79-
Domains map[string]*Domain
86+
Domains map[string]*Domain
87+
88+
configPath string
89+
addresses []string
8090
rootDomain *Domain
8191
singleDomain *Domain
82-
addresses []string
83-
configPath string
92+
vfs *vfs.VFS
8493
config *config.Config
8594
appCfg *config.Config
8695
}
8796

8897
// Load method loads a configuration from given file e.g. `routes.conf` and
8998
// applies env profile override values if available.
9099
func (r *Router) Load() (err error) {
91-
if !ess.IsFileExists(r.configPath) {
100+
if !r.vfs.IsExists(r.configPath) {
92101
return fmt.Errorf("router: configuration does not exists: %v", r.configPath)
93102
}
94103

95-
r.config, err = config.LoadFile(r.configPath)
104+
r.config, err = config.VFSLoadFile(r.vfs, r.configPath)
96105
if err != nil {
97106
return err
98107
}
@@ -184,9 +193,9 @@ func (r *Router) RegisteredWSActions() map[string]map[string]uint8 {
184193
return methods
185194
}
186195

187-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
196+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
188197
// Router unexpoted methods
189-
//___________________________________
198+
//______________________________________________________________________________
190199

191200
func (r *Router) processRoutesConfig() (err error) {
192201
domains := r.config.KeysByPath("domains")
@@ -353,9 +362,9 @@ func (r *Router) processRoutes(domain *Domain, domainCfg *config.Config) error {
353362
return nil
354363
}
355364

356-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
365+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
357366
// Route
358-
//___________________________________
367+
//______________________________________________________________________________
359368

360369
// Route holds the single route details.
361370
type Route struct {
@@ -404,9 +413,9 @@ func (r *Route) ValidationRule(name string) (string, bool) {
404413
return rules, found
405414
}
406415

407-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
416+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
408417
// Unexported methods
409-
//___________________________________
418+
//______________________________________________________________________________
410419

411420
func parseRoutesSection(cfg *config.Config, routeInfo *parentRouteInfo) (routes []*Route, err error) {
412421
for _, routeName := range cfg.Keys() {

router_test.go

+23-31
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"aahframework.org/config.v0"
1717
"aahframework.org/essentials.v0"
1818
"aahframework.org/test.v0/assert"
19+
"aahframework.org/vfs.v0"
1920
)
2021

2122
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
@@ -402,10 +403,7 @@ func TestRouterConfigNotExists(t *testing.T) {
402403
}
403404

404405
func TestRouterNamespaceConfig(t *testing.T) {
405-
wd, _ := os.Getwd()
406-
appCfg, _ := config.ParseString("")
407-
router := New(filepath.Join(wd, "testdata", "routes-namespace.conf"), appCfg)
408-
err := router.Load()
406+
router, err := createRouter("routes-namespace.conf")
409407
assert.FailNowOnError(t, err, "")
410408

411409
routes := router.Domains["localhost:8080"].routes
@@ -418,18 +416,14 @@ func TestRouterNamespaceConfig(t *testing.T) {
418416
assert.Equal(t, "GET", routes["disable_user"].Method)
419417
assert.Equal(t, "form", routes["disable_user"].Auth)
420418

421-
router = New(filepath.Join(wd, "testdata", "routes-namespace-action-error.conf"), appCfg)
422-
err = router.Load()
419+
// Error
420+
_, err = createRouter("routes-namespace-action-error.conf")
423421
assert.NotNil(t, err)
424422
assert.Equal(t, "'list_users.action' key is missing, it seems to be multiple HTTP methods", err.Error())
425423
}
426424

427425
func TestRouterNamespaceSimplifiedConfig(t *testing.T) {
428-
429-
wd, _ := os.Getwd()
430-
appCfg, _ := config.ParseString("")
431-
router := New(filepath.Join(wd, "testdata", "routes-simplified.conf"), appCfg)
432-
err := router.Load()
426+
router, err := createRouter("routes-simplified.conf")
433427
assert.FailNowOnError(t, err, "")
434428

435429
routes := router.Domains["localhost:8080"].routes
@@ -450,11 +444,7 @@ func TestRouterNamespaceSimplifiedConfig(t *testing.T) {
450444
}
451445

452446
func TestRouterNamespaceSimplified2Config(t *testing.T) {
453-
454-
wd, _ := os.Getwd()
455-
appCfg, _ := config.ParseString("")
456-
router := New(filepath.Join(wd, "testdata", "routes-simplified-2.conf"), appCfg)
457-
err := router.Load()
447+
router, err := createRouter("routes-simplified-2.conf")
458448
assert.FailNowOnError(t, err, "")
459449

460450
routes := router.Domains["localhost:8080"].routes
@@ -474,18 +464,13 @@ func TestRouterNamespaceSimplified2Config(t *testing.T) {
474464
assert.Equal(t, "gt=1,lt=10", rule)
475465

476466
// Error
477-
router = New(filepath.Join(wd, "testdata", "routes-simplified-2-error.conf"), appCfg)
478-
err = router.Load()
467+
_, err = createRouter("routes-simplified-2-error.conf")
479468
assert.NotNil(t, err)
480469
assert.Equal(t, "'routes.path' has invalid validation rule '/v1/users/:id gt=1,lt=10]'", err.Error())
481470
}
482471

483472
func TestRouterStaticSectionBaseDirForFilePaths(t *testing.T) {
484-
485-
wd, _ := os.Getwd()
486-
appCfg, _ := config.ParseString("")
487-
router := New(filepath.Join(wd, "testdata", "routes-static.conf"), appCfg)
488-
err := router.Load()
473+
router, err := createRouter("routes-static.conf")
489474
assert.FailNowOnError(t, err, "")
490475

491476
// Assertion
@@ -506,17 +491,13 @@ func TestRouterStaticSectionBaseDirForFilePaths(t *testing.T) {
506491
assert.Equal(t, "robots.txt", robotTxtRoute.File)
507492

508493
// ERROR missing values
509-
router = New(filepath.Join(wd, "testdata", "routes-static-base-dir-missing.conf"), appCfg)
510-
err = router.Load()
494+
_, err = createRouter("routes-static-base-dir-missing.conf")
511495
assert.NotNil(t, err)
512496
assert.Equal(t, "'static.favicon.base_dir' value is missing", err.Error())
513497
}
514498

515499
func TestRouterWebSocketConfig(t *testing.T) {
516-
wd, _ := os.Getwd()
517-
appCfg, _ := config.ParseString("")
518-
router := New(filepath.Join(wd, "testdata", "routes-websocket.conf"), appCfg)
519-
err := router.Load()
500+
router, err := createRouter("routes-websocket.conf")
520501
assert.FailNowOnError(t, err, "")
521502

522503
routes := router.Domains["localhost:8080"].routes
@@ -538,17 +519,20 @@ func TestRouterWebSocketConfig(t *testing.T) {
538519
}
539520

540521
func createRouter(filename string) (*Router, error) {
522+
fs := new(vfs.VFS)
523+
fs.AddMount("/app/config", testdataBaseDir())
541524

542-
wd, _ := os.Getwd()
543525
appCfg, _ := config.ParseString(`routes {
544526
localhost {
545527
host = "localhost"
546528
port = "8080"
547529
}
548530
}`)
549531

550-
router := New(filepath.Join(wd, "testdata", filename), appCfg)
532+
router := New("/app/config/"+filename, appCfg)
533+
router.vfs = fs
551534
err := router.Load()
535+
552536
return router, err
553537
}
554538

@@ -563,3 +547,11 @@ func createHTTPRequest(host, path string) *http.Request {
563547

564548
return req
565549
}
550+
551+
func testdataBaseDir() string {
552+
wd, _ := os.Getwd()
553+
if idx := strings.Index(wd, "testdata"); idx > 0 {
554+
wd = wd[:idx]
555+
}
556+
return filepath.Join(wd, "testdata")
557+
}

0 commit comments

Comments
 (0)