Skip to content

Commit

Permalink
👷 Extract "USE" to a global const variable
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyonlin committed Jul 12, 2020
1 parent 39fdf8b commit 2cfc1b1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
5 changes: 4 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ func methodInt(s string) int {
return 7
case MethodPatch:
return 8
case methodUse:
return 9
default:
return 0
return -1
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2cfc1b1

Please sign in to comment.