From b4a250d85714e5e02fc42050ebd03a6485aea756 Mon Sep 17 00:00:00 2001 From: Neeraj319 Date: Tue, 26 Dec 2023 12:59:12 +0545 Subject: [PATCH] feat: changed router add logic --- router.go | 52 +++++++++++++++++++++++++++++++++++++++++++--------- server.go | 12 +++++++++++- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/router.go b/router.go index 4951c74..dc45f48 100644 --- a/router.go +++ b/router.go @@ -2,6 +2,7 @@ package main import ( // "fmt" + "fmt" "net/http" "reflect" "runtime" @@ -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 @@ -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) @@ -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 } @@ -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 { @@ -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 diff --git a/server.go b/server.go index 75f5db2..31b3174 100644 --- a/server.go +++ b/server.go @@ -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") } @@ -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 {