From 183237e24b95ee66d6047bfdceae5daf2c09cccd Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Wed, 21 Jun 2017 23:19:55 -0700 Subject: [PATCH] go-aah/aah#37 auth argument added to routes --- radix_tree.go | 2 +- router.go | 30 ++++++++++++++++++++++-------- router_test.go | 2 ++ testdata/routes-namespace.conf | 1 + testdata/routes.conf | 7 +++++++ 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/radix_tree.go b/radix_tree.go index f72d458..c315b7f 100644 --- a/radix_tree.go +++ b/radix_tree.go @@ -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 diff --git a/router.go b/router.go index fac6b18..6e400ae 100644 --- a/router.go +++ b/router.go @@ -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 @@ -82,6 +81,7 @@ type ( Controller string Action string ParentName string + Auth string // static route fields in-addition to above IsStatic bool @@ -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 + } ) //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ @@ -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) } } @@ -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 } @@ -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") @@ -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 @@ -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{ @@ -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 diff --git a/router_test.go b/router_test.go index 18615cb..bbbf188 100644 --- a/router_test.go +++ b/router_test.go @@ -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() diff --git a/testdata/routes-namespace.conf b/testdata/routes-namespace.conf index 31c522d..3007569 100644 --- a/testdata/routes-namespace.conf +++ b/testdata/routes-namespace.conf @@ -5,6 +5,7 @@ domains { routes { v1_api { path = "/v1" + auth = "form" routes { list_users { diff --git a/testdata/routes.conf b/testdata/routes.conf index 795f50b..e2870a4 100644 --- a/testdata/routes.conf +++ b/testdata/routes.conf @@ -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