1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-07 04:51:34 +00:00

👷 Extract "USE" to a global const variable

This commit is contained in:
kiyon 2020-07-12 23:54:22 +08:00
parent 39fdf8bfa2
commit 2cfc1b1546
4 changed files with 12 additions and 9 deletions

8
app.go
View File

@ -297,7 +297,7 @@ func (app *App) Use(args ...interface{}) *Route {
log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg))
}
}
return app.register("USE", prefix, handlers...)
return app.register(methodUse, prefix, handlers...)
}
// Get registers a route for GET methods that requests a representation
@ -375,7 +375,7 @@ func (app *App) All(path string, handlers ...Handler) []*Route {
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
func (app *App) Group(prefix string, handlers ...Handler) *Group {
if len(handlers) > 0 {
app.register("USE", prefix, handlers...)
app.register(methodUse, prefix, handlers...)
}
return &Group{prefix: prefix, app: app}
}
@ -408,10 +408,10 @@ func (app *App) Routes() []*Route {
continue
}
// Don't duplicate USE routes
if app.stack[m][r].Method == "USE" {
if app.stack[m][r].Method == methodUse {
duplicate := false
for i := range routes {
if routes[i].Method == "USE" && routes[i].Name == app.stack[m][r].Name {
if routes[i].Method == methodUse && routes[i].Name == app.stack[m][r].Name {
duplicate = true
}
}

View File

@ -35,7 +35,7 @@ func (grp *Group) Use(args ...interface{}) *Route {
log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg))
}
}
return grp.app.register("USE", getGroupPath(grp.prefix, path), handlers...)
return grp.app.register(methodUse, getGroupPath(grp.prefix, path), handlers...)
}
// Get registers a route for GET methods that requests a representation
@ -114,7 +114,7 @@ func (grp *Group) All(path string, handlers ...Handler) []*Route {
func (grp *Group) Group(prefix string, handlers ...Handler) *Group {
prefix = getGroupPath(grp.prefix, prefix)
if len(handlers) > 0 {
grp.app.register("USE", prefix, handlers...)
grp.app.register(methodUse, prefix, handlers...)
}
return grp.app.Group(prefix)
}

View File

@ -146,7 +146,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) *Route {
// Uppercase HTTP methods
method = utils.ToUpper(method)
// Check if the HTTP method is valid unless it's USE
if method != "USE" && methodInt(method) == 0 && method != MethodGet {
if methodInt(method) == -1 {
log.Fatalf("Add: Invalid HTTP method %s", method)
}
// A route requires atleast one ctx handler
@ -172,7 +172,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) *Route {
pathPretty = utils.TrimRight(pathPretty, '/')
}
// Is layer a middleware?
var isUse = method == "USE"
var isUse = method == methodUse
// Is path a direct wildcard?
var isStar = pathPretty == "/*"
// Is path a root slash?

View File

@ -241,8 +241,10 @@ func methodInt(s string) int {
return 7
case MethodPatch:
return 8
case methodUse:
return 9
default:
return 0
return -1
}
}
@ -270,6 +272,7 @@ const (
MethodConnect = "CONNECT" // RFC 7231, 4.3.6
MethodOptions = "OPTIONS" // RFC 7231, 4.3.7
MethodTrace = "TRACE" // RFC 7231, 4.3.8
methodUse = "USE"
)
// MIME types that are commonly used