Skip to content

Commit

Permalink
feat: changed router add logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Dec 26, 2023
1 parent d094687 commit b4a250d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
52 changes: 43 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
// "fmt"
"fmt"
"net/http"
"reflect"
"runtime"
Expand Down Expand Up @@ -51,6 +52,15 @@ func defaultMethodNotAllowedResp(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(405)
}

func valueIn(i int, dict map[string]int) bool {
for _, value := range dict {
if i == value {
return true
}
}
return false
}

type RouteHandler struct {
pathParams map[string]int
route string
Expand All @@ -76,6 +86,37 @@ func CreateRouter() *SimpleRouter {
return &router
}

func removeSimilarRoute(r *map[*RouteHandler]HandlerFunction, routeHandler RouteHandler) {
actualMap := *r
keysToDelete := make([]*RouteHandler, 0)

for routeObj := range actualMap {
if isRouteSimilar(routeObj, routeHandler) {
keysToDelete = append(keysToDelete, routeObj)
}
}

for _, key := range keysToDelete {
delete(actualMap, key)
}
}

func isRouteSimilar(routeObj *RouteHandler, routeHandler RouteHandler) bool {
return routeObj.route == routeHandler.route &&
routeObj.http_method == routeHandler.http_method &&
len(routeObj.pathParams) == len(routeHandler.pathParams) &&
areAllParamsPresent(routeObj.pathParams, routeHandler.pathParams)
}

func areAllParamsPresent(objParams map[string]int, handlerParams map[string]int) bool {
for _, param := range objParams {
if !valueIn(param, handlerParams) {
return false
}
}
return true
}

func (r *SimpleRouter) addRoute(path string, function HandlerFunction, http_method HTTP_METHOD) {
pathParams := make(map[string]int)
routeHandler := createRouteHandler(http_method)
Expand All @@ -98,6 +139,7 @@ func (r *SimpleRouter) addRoute(path string, function HandlerFunction, http_meth
}
routeHandler.pathParams = pathParams
routeHandler.route = path
removeSimilarRoute(&r.routeMapping, *routeHandler)
r.routeMapping[routeHandler] = function
}

Expand All @@ -110,15 +152,6 @@ func removeIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}

func valueIn(i int, dict map[string]int) bool {
for _, value := range dict {
if i == value {
return true
}
}
return false
}

func extractPath(urlArray []string, param *RouteHandler) string {
var path string
for i, value := range urlArray {
Expand Down Expand Up @@ -212,6 +245,7 @@ func (r *SimpleRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
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
12 changes: 11 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func HomeSomePath(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/home/:somePath")
}

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")
}
Expand All @@ -54,15 +57,22 @@ func main() {
r.addRoute("/", Index, GET)
r.addRoute("/:anyParam", Hello, POST)
r.addRoute("/:anyParam", Hello, GET)
r.addRoute("/home/:somePath", HomeSomePath, PATCH)
r.addRoute("/home/:somePath", HomeSomePath, POST)
r.addRoute("/home/:somePath", HomeSomePathPatch, PATCH)
r.addRoute("/random", Random, PATCH)
r.addRoute("/random/:params", RandomWithParams, DELETE)
r.addRoute("/random/:params", RandomWithParams, POST)
r.addRoute("/:somePath/asdf/:nothing", HomeSomePath2, CONNECT)
r.addRoute("/asdf/:fasdfasd/:jflakdsfas", HomeSomePath2, CONNECT)
r.addRoute("/file/:fileName/:anotherParam", FileNotRandom, OPTIONS)
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 b4a250d

Please sign in to comment.