Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Oct 9, 2018
1 parent 183e58e commit f3fc330
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,48 @@ func (rtr *Router) Use(f func(http.ResponseWriter, *http.Request, http.HandlerFu

// NewRouter initializes returns a new router instance with all the configurations and routes set
func NewRouter(cfg *Config, routes []*Route) *Router {
handlers := httpHandlers(routes)
r := &Router{
optHandlers: handlers[http.MethodOptions],
headHandlers: handlers[http.MethodHead],
getHandlers: handlers[http.MethodGet],
postHandlers: handlers[http.MethodPost],
putHandlers: handlers[http.MethodPut],
patchHandlers: handlers[http.MethodPatch],
deleteHandlers: handlers[http.MethodDelete],

NotFound: http.NotFound,
AppContext: make(map[string]interface{}, 0),
config: cfg,
}
// setting the default serve handler
r.serveHandler = r.serve

return r
}

// checkDuplicateRoutes checks if any of the routes have duplicate name or URI pattern
func checkDuplicateRoutes(idx int, route *Route, routes []*Route) {
// checking if the URI pattern is duplicated
for i := 0; i < idx; i++ {
rt := routes[i]

if rt.Name == route.Name {
warnLogger.Println("Duplicate route name(\"" + rt.Name + "\") detected. Route name should be unique.")
}

if rt.Method == route.Method {
// regex pattern match
if ok, _ := rt.matchAndGet(route.Pattern); ok {
warnLogger.Println("Duplicate URI pattern detected.\nPattern: '" + rt.Pattern + "'\nDuplicate pattern: '" + route.Pattern + "'")
infoLogger.Println("Only the first route to match the URI pattern would handle the request")
}
}
}
}

// httpHandlers returns all the handlers in a map, for each HTTP method
func httpHandlers(routes []*Route) map[string][]*Route {
handlers := make(map[string][]*Route, len(validHTTPMethods))

for _, validMethod := range validHTTPMethods {
Expand All @@ -311,57 +353,30 @@ func NewRouter(cfg *Config, routes []*Route) *Router {
for _, validMethod := range validHTTPMethods {
if route.Method == validMethod {
found = true
break
}
}

if !found {
errLogger.Fatalln("Unsupported HTTP request method provided. Method:", route.Method)
return nil
}

if route.Handlers == nil || len(route.Handlers) == 0 {
errLogger.Fatalln("No handlers provided for the route '", route.Pattern, "', method '", route.Method, "'")
return nil
}

err := route.init()
if err != nil {
errLogger.Fatalln("Unsupported URI pattern.", route.Pattern, err)
return nil
}

// checking if the URI pattern is duplicated
for i := 0; i < idx; i++ {
rt := routes[i]

if rt.Name == route.Name {
warnLogger.Println("Duplicate route name(\"" + rt.Name + "\") detected. Route name should be unique.")
}

if rt.Method == route.Method {
// regex pattern match
if ok, _ := rt.matchAndGet(route.Pattern); ok {
warnLogger.Println("Duplicate URI pattern detected.\nPattern: '" + rt.Pattern + "'\nDuplicate pattern: '" + route.Pattern + "'")
infoLogger.Println("Only the first route to match the URI pattern would handle the request")
}
}
}
checkDuplicateRoutes(idx, route, routes)

handlers[route.Method] = append(handlers[route.Method], route)
}

r := &Router{
optHandlers: handlers[http.MethodOptions],
headHandlers: handlers[http.MethodHead],
getHandlers: handlers[http.MethodGet],
postHandlers: handlers[http.MethodPost],
putHandlers: handlers[http.MethodPut],
patchHandlers: handlers[http.MethodPatch],
deleteHandlers: handlers[http.MethodDelete],

NotFound: http.NotFound,
AppContext: make(map[string]interface{}, 0),
config: cfg,
}
// setting the default serve handler
r.serveHandler = r.serve

return r
return handlers
}

0 comments on commit f3fc330

Please sign in to comment.