Skip to content

Commit

Permalink
feat: changed add route logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Dec 26, 2023
1 parent b4a250d commit 44adb07
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
13 changes: 5 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ func extractPath(urlArray []string, param *RouteHandler) string {
return path
}

func getSingleSlashHandler(params []*RouteHandler, urlArray []string) (*RouteHandler, bool) {
func getSingleSlashHandlers(params []*RouteHandler, urlArray []string) []*RouteHandler {

routeHandlers := make([]*RouteHandler, 0)
for _, routeHandlerObj := range params {
if len(urlArray) == len(routeHandlerObj.pathParams) {
return routeHandlerObj, true
routeHandlers = append(routeHandlers, routeHandlerObj)
}
}
return &RouteHandler{}, false
return routeHandlers
}

func removeBlankStrings(array []string) []string {
Expand Down Expand Up @@ -235,17 +236,13 @@ func (r *SimpleRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
*/

if len(possibleRouteHandlers) == 0 && len(slashMap) > 0 {
routeObj, ok := getSingleSlashHandler(slashMap, pathArray)
if ok {
possibleRouteHandlers = append(possibleRouteHandlers, routeObj)
}
possibleRouteHandlers = getSingleSlashHandlers(slashMap, pathArray)
}
if len(possibleRouteHandlers) == 0 {
r.NotFoundResp(w, req)
return
}
for _, routeObj := range possibleRouteHandlers {
fmt.Println("possible", routeObj.http_method, routeObj.route, routeObj.pathParams)
if routeObj.http_method.String() == req.Method {
r.routeMapping[routeObj](w, req)
return
Expand Down
16 changes: 7 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func HomeSomePath(w http.ResponseWriter, r *http.Request) {
func HomeSomePathPatch(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/home/:somePath, patch")
}
func Hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/:anyParam")
func HelloPost(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/:anyParam, post")
}

func HelloPatch(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/:anyParam, Patch")
}
func Random(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/random")
}
Expand All @@ -55,8 +58,8 @@ func main() {
r.addRoute("/home", Random, POST)
r.addRoute("/home", HomePost, POST)
r.addRoute("/", Index, GET)
r.addRoute("/:anyParam", Hello, POST)
r.addRoute("/:anyParam", Hello, GET)
r.addRoute("/:anyParam", HelloPost, POST)
r.addRoute("/:anyParam", HelloPatch, PATCH)
r.addRoute("/home/:somePath", HomeSomePath, POST)
r.addRoute("/home/:somePath", HomeSomePathPatch, PATCH)
r.addRoute("/random", Random, PATCH)
Expand All @@ -68,11 +71,6 @@ func main() {
r.addRoute("/file/:fileName/random/:somethingElse", FileRandom, TRACE)

fmt.Println("Started listening on 0.0.0.0:8080")
for routeHandler, function := range r.routeMapping {
fmt.Printf("%s, %v, %s, %s\n",
routeHandler.route, routeHandler.pathParams, routeHandler.http_method, GetFunctionName(function))
}
fmt.Println("------------------------------->")
err := http.ListenAndServe("0.0.0.0:8080", r)

if err != nil {
Expand Down

0 comments on commit 44adb07

Please sign in to comment.