🖇 Cleanup router chaining

pull/643/head
Fenny 2020-07-19 13:36:26 +02:00
parent ea7429eaf7
commit f9e6ea8103
3 changed files with 10 additions and 13 deletions

6
app.go
View File

@ -300,7 +300,7 @@ func (app *App) Use(args ...interface{}) Router {
panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg))) panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
} }
} }
_ = app.register(methodUse, prefix, handlers...) app.register(methodUse, prefix, handlers...)
return app return app
} }
@ -359,13 +359,13 @@ func (app *App) Patch(path string, handlers ...Handler) Router {
// Add ... // Add ...
func (app *App) Add(method, path string, handlers ...Handler) Router { func (app *App) Add(method, path string, handlers ...Handler) Router {
_ = app.register(method, path, handlers...) app.register(method, path, handlers...)
return app return app
} }
// Static ... // Static ...
func (app *App) Static(prefix, root string, config ...Static) Router { func (app *App) Static(prefix, root string, config ...Static) Router {
_ = app.registerStatic(prefix, root, config...) app.registerStatic(prefix, root, config...)
return app return app
} }

View File

@ -35,7 +35,7 @@ func (grp *Group) Use(args ...interface{}) Router {
panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg))) panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
} }
} }
_ = grp.app.register(methodUse, getGroupPath(grp.prefix, path), handlers...) grp.app.register(methodUse, getGroupPath(grp.prefix, path), handlers...)
return grp return grp
} }
@ -94,13 +94,13 @@ func (grp *Group) Patch(path string, handlers ...Handler) Router {
// Add ... // Add ...
func (grp *Group) Add(method, path string, handlers ...Handler) Router { func (grp *Group) Add(method, path string, handlers ...Handler) Router {
_ = grp.app.register(method, getGroupPath(grp.prefix, path), handlers...) grp.app.register(method, getGroupPath(grp.prefix, path), handlers...)
return grp return grp
} }
// Static ... // Static ...
func (grp *Group) Static(prefix, root string, config ...Static) Router { func (grp *Group) Static(prefix, root string, config ...Static) Router {
_ = grp.app.registerStatic(getGroupPath(grp.prefix, prefix), root, config...) grp.app.registerStatic(getGroupPath(grp.prefix, prefix), root, config...)
return grp return grp
} }
@ -116,7 +116,7 @@ func (grp *Group) All(path string, handlers ...Handler) Router {
func (grp *Group) Group(prefix string, handlers ...Handler) Router { func (grp *Group) Group(prefix string, handlers ...Handler) Router {
prefix = getGroupPath(grp.prefix, prefix) prefix = getGroupPath(grp.prefix, prefix)
if len(handlers) > 0 { if len(handlers) > 0 {
_ = grp.app.register(methodUse, prefix, handlers...) grp.app.register(methodUse, prefix, handlers...)
} }
return grp.app.Group(prefix) return grp.app.Group(prefix)
} }

View File

@ -142,7 +142,7 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
app.ReleaseCtx(ctx) app.ReleaseCtx(ctx)
} }
func (app *App) register(method, pathRaw string, handlers ...Handler) *Route { func (app *App) register(method, pathRaw string, handlers ...Handler) {
// Uppercase HTTP methods // Uppercase HTTP methods
method = utils.ToUpper(method) method = utils.ToUpper(method)
// Check if the HTTP method is valid unless it's USE // Check if the HTTP method is valid unless it's USE
@ -208,7 +208,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) *Route {
for _, m := range intMethod { for _, m := range intMethod {
app.addRoute(m, route) app.addRoute(m, route)
} }
return route return
} }
// Handle GET routes on HEAD requests // Handle GET routes on HEAD requests
@ -218,11 +218,9 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) *Route {
// Add route to stack // Add route to stack
app.addRoute(method, route) app.addRoute(method, route)
return route
} }
func (app *App) registerStatic(prefix, root string, config ...Static) *Route { func (app *App) registerStatic(prefix, root string, config ...Static) {
// For security we want to restrict to the current work directory. // For security we want to restrict to the current work directory.
if len(root) == 0 { if len(root) == 0 {
root = "." root = "."
@ -323,7 +321,6 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
// Add route to stack // Add route to stack
app.addRoute(MethodGet, route) app.addRoute(MethodGet, route)
app.addRoute(MethodHead, route) app.addRoute(MethodHead, route)
return route
} }
func (app *App) addRoute(method string, route *Route) { func (app *App) addRoute(method string, route *Route) {