Skip to content

Commit

Permalink
go-aah/aah#37 auth argument added to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 22, 2017
1 parent 30cfea6 commit 183237e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion radix_tree.go
Expand Up @@ -151,7 +151,7 @@ func (n *node) add(path string, value interface{}) error {
if n.nType == catchAll {
pathSeg = path
} else {
pathSeg = strings.SplitN(path, "/", 2)[0]
pathSeg = strings.SplitN(path, SlashString, 2)[0]
}
prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path

Expand Down
30 changes: 22 additions & 8 deletions router.go
Expand Up @@ -57,7 +57,6 @@ type (
configPath string
config *config.Config
appCfg *config.Config
// isConfigLoad bool
}

// Domain is used to hold domain related routes and it's route configuration
Expand All @@ -82,6 +81,7 @@ type (
Controller string
Action string
ParentName string
Auth string

// static route fields in-addition to above
IsStatic bool
Expand All @@ -98,6 +98,12 @@ type (

// PathParams is a PathParam-slice, as returned by the route tree.
PathParams []PathParam

parentRouteInfo struct {
ParentName string
PrefixPath string
Auth string
}
)

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand Down Expand Up @@ -300,8 +306,8 @@ func (r *Router) processRoutesConfig() (err error) {
if !ess.IsStrEmpty(dr.ParentName) {
parentInfo = fmt.Sprintf("(parent: %s)", dr.ParentName)
}
log.Tracef("Route Name: %v %v, Path: %v, Method: %v, Controller: %v, Action: %v",
dr.Name, parentInfo, dr.Path, dr.Method, dr.Controller, dr.Action)
log.Tracef("Route Name: %v %v, Path: %v, Method: %v, Controller: %v, Action: %v, Auth: %v",
dr.Name, parentInfo, dr.Path, dr.Method, dr.Controller, dr.Action, dr.Auth)
}
}

Expand Down Expand Up @@ -343,7 +349,7 @@ func (r *Router) processRoutes(domain *Domain, domainCfg *config.Config) error {
return nil
}

routes, err := parseRoutesSection(routesCfg, "", "")
routes, err := parseRoutesSection(routesCfg, &parentRouteInfo{})
if err != nil {
return err
}
Expand Down Expand Up @@ -630,7 +636,7 @@ func addRegisteredAction(methods map[string]map[string]uint8, route *Route) {
}
}

func parseRoutesSection(cfg *config.Config, parentName, prefixPath string) (routes []*Route, err error) {
func parseRoutesSection(cfg *config.Config, routeInfo *parentRouteInfo) (routes []*Route, err error) {
for _, routeName := range cfg.Keys() {
// getting 'path'
routePath, found := cfg.String(routeName + ".path")
Expand All @@ -645,7 +651,7 @@ func parseRoutesSection(cfg *config.Config, parentName, prefixPath string) (rout
return
}

routePath = path.Join(prefixPath, routePath)
routePath = path.Join(routeInfo.PrefixPath, routePath)

// check child routes exists
notToSkip := true
Expand Down Expand Up @@ -674,6 +680,9 @@ func parseRoutesSection(cfg *config.Config, parentName, prefixPath string) (rout
return
}

// getting route authentication scheme name
routeAuth := cfg.StringDefault(routeName+".auth", routeInfo.Auth)

if notToSkip {
for _, m := range strings.Split(routeMethod, ",") {
routes = append(routes, &Route{
Expand All @@ -682,14 +691,19 @@ func parseRoutesSection(cfg *config.Config, parentName, prefixPath string) (rout
Method: strings.TrimSpace(m),
Controller: routeController,
Action: routeAction,
ParentName: parentName,
ParentName: routeInfo.ParentName,
Auth: routeAuth,
})
}
}

// loading child routes
if childRoutes, found := cfg.GetSubConfig(routeName + ".routes"); found {
croutes, er := parseRoutesSection(childRoutes, routeName, routePath)
croutes, er := parseRoutesSection(childRoutes, &parentRouteInfo{
ParentName: routeName,
PrefixPath: routePath,
Auth: routeAuth,
})
if er != nil {
err = er
return
Expand Down
2 changes: 2 additions & 0 deletions router_test.go
Expand Up @@ -426,8 +426,10 @@ func TestRouterNamespaceConfig(t *testing.T) {
assert.Equal(t, 4, len(routes))
assert.Equal(t, "/v1/users", routes["create_user"].Path)
assert.Equal(t, "POST", routes["create_user"].Method)
assert.Equal(t, "form", routes["create_user"].Auth)
assert.Equal(t, "/v1/users/:id/settings", routes["disable_user"].Path)
assert.Equal(t, "GET", routes["disable_user"].Method)
assert.Equal(t, "form", routes["disable_user"].Auth)

router = New(filepath.Join(wd, "testdata", "routes-namespace-action-error.conf"), appCfg)
err = router.Load()
Expand Down
1 change: 1 addition & 0 deletions testdata/routes-namespace.conf
Expand Up @@ -5,6 +5,7 @@ domains {
routes {
v1_api {
path = "/v1"
auth = "form"

routes {
list_users {
Expand Down
7 changes: 7 additions & 0 deletions testdata/routes.conf
Expand Up @@ -144,6 +144,13 @@ domains {
action = "EditSettings"
}

hotel_settings_options {
path = "/settings"
method = "OPTIONS"
controller = "Hotel"
action = "Settings"
}

} # end of application routes

} # end of domain routes localhost
Expand Down

0 comments on commit 183237e

Please sign in to comment.