Skip to content

Commit

Permalink
fix: bug with method finding
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Dec 25, 2023
1 parent 85398dc commit 987dd70
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work

tmp/
3 changes: 1 addition & 2 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Nirajan

Router for the net/http module

Another router for the net/http module



Expand Down
36 changes: 21 additions & 15 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,37 +180,43 @@ func (r *SimpleRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
pathArray := strings.Split(url, "/")
pathArray = removeBlankStrings(pathArray)

index := 0
var slashMap []RouteHandler
var routeHandler RouteHandler
var possibleRouteHandlers []RouteHandler

for pathString, routeHandlers := range r.routeMapping {
if routeHandler.function != nil {
if len(possibleRouteHandlers) != 0 {
break
}
if pathString == "/" {
slashMap = routeHandlers
continue
}

for _, routeObj := range routeHandlers {
finalPath := extractPath(pathArray, routeObj)
paramValueMap := getParams(pathArray, routeObj)
if finalPath == pathString && (len(paramValueMap) == len(routeObj.pathParams)) {
routeHandler = routeObj
break
possibleRouteHandlers = append(possibleRouteHandlers, routeObj)
}
}
if index == len(r.routeMapping)-1 && len(slashMap) > 0 {
routeHandler = getSingleSlashHandler(slashMap, pathArray)
}
index++
}
if routeHandler.function != nil {
if routeHandler.method.String() != req.Method {
r.MethodNotAllowedResp(w, req)
}
routeHandler.function(w, req)
} else {
/*
if there are routes with only "/" and if route is not found then check in "/" routes
for this only check for the length of the param
*/

if len(possibleRouteHandlers) == 0 && len(slashMap) > 0 {
possibleRouteHandlers = append(possibleRouteHandlers, getSingleSlashHandler(slashMap, pathArray))
}
if len(possibleRouteHandlers) == 0 {
r.NotFoundResp(w, req)
return
}
for _, routeObj := range possibleRouteHandlers {
if routeObj.method.String() == req.Method {
routeObj.function(w, req)
return
}
}
r.MethodNotAllowedResp(w, req)
}
5 changes: 5 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ func RandomWithParams(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/random/:params")
}

func HomePost(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "home post")
}

func main() {
r := CreateRouter()
r.addRoute("/home", Home, GET)
r.addRoute("/home", HomePost, POST)
r.addRoute("/", Index, GET)
r.addRoute("/:anyParam", Hello, POST)
r.addRoute("/home/:somePath", HomeSomePath, PATCH)
Expand Down

0 comments on commit 987dd70

Please sign in to comment.