Support Naming Routes

pull/509/head
Fenny 2020-06-25 16:24:21 +02:00
parent 4aafc504ad
commit 85d7a6add4
2 changed files with 19 additions and 1 deletions

13
app.go
View File

@ -386,8 +386,21 @@ func (app *App) Routes() []*Route {
if m == 1 && app.stack[m][r].Method == MethodGet { if m == 1 && app.stack[m][r].Method == MethodGet {
continue continue
} }
// Don't duplicate USE routes
if app.stack[m][r].Method == "USE" {
duplicate := false
for i := range routes {
if routes[i].Method == "USE" && routes[i].Name == app.stack[m][r].Name {
duplicate = true
}
}
if !duplicate {
routes = append(routes, app.stack[m][r]) routes = append(routes, app.stack[m][r])
} }
} else {
routes = append(routes, app.stack[m][r])
}
}
} }
// Sort routes by stack position // Sort routes by stack position
sort.Slice(routes, func(i, k int) bool { sort.Slice(routes, func(i, k int) bool {

View File

@ -46,6 +46,7 @@ type Route struct {
routeParams []string // Case sensitive param keys routeParams []string // Case sensitive param keys
// Public fields // Public fields
Name string // Name of first handler used in route
Path string // Original registered route path Path string // Original registered route path
Method string // HTTP method Method string // HTTP method
Handlers []Handler // Ctx handlers Handlers []Handler // Ctx handlers
@ -319,6 +320,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
} }
func (app *App) addRoute(method string, route *Route) { func (app *App) addRoute(method string, route *Route) {
// Give name to route if not defined
if route.Name == "" && len(route.Handlers) > 0 {
route.Name = utils.FunctionName(route.Handlers[0])
}
// Get unique HTTP method indentifier // Get unique HTTP method indentifier
m := methodINT[method] m := methodINT[method]
// Add route to the stack // Add route to the stack