From 41ffa82cbc7302b18bf1c8e45970684c8770ef48 Mon Sep 17 00:00:00 2001 From: Kevin Mathew Sunny Date: Fri, 25 Oct 2024 01:08:15 +0530 Subject: [PATCH] refactor: chagned app.Route to app.methodName --- examples/crud/main.go | 10 +++++----- examples/http_header/main.go | 2 +- examples/json_data/main.go | 2 +- examples/simple_route/main.go | 2 +- router/router.go | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/examples/crud/main.go b/examples/crud/main.go index 8b6fc9f..25770fa 100644 --- a/examples/crud/main.go +++ b/examples/crud/main.go @@ -149,11 +149,11 @@ func main() { app.Use(loggingMiddleware) // Register routes - app.Route("GET", "/hello", HelloHandler) - app.Route("POST", "/users/create", CreateUser) - app.Route("GET", "/users/get", GetUser) - app.Route("PUT", "/users/update", UpdateUser) - app.Route("DELETE", "/users/delete", DeleteUser) + app.Get("/hello", HelloHandler) + app.Post("/users/create", CreateUser) + app.Get("/users/get", GetUser) + app.Put("/users/update", UpdateUser) + app.Delete("/users/delete", DeleteUser) fmt.Println("Server listening on port 3333") app.Listen(":3333") diff --git a/examples/http_header/main.go b/examples/http_header/main.go index ca67186..51ec380 100644 --- a/examples/http_header/main.go +++ b/examples/http_header/main.go @@ -14,7 +14,7 @@ func main() { app := router.NewApp() // Define a route that responds to a GET request at "/auth". - app.Route("GET", "/auth", func(r *req.Request, w *req.Response) { + app.Get("/auth", func(r *req.Request, w *req.Response) { authHeader := r.Header("Authorization") if authHeader == "" { w.Status(401).Send("Unauthorized") diff --git a/examples/json_data/main.go b/examples/json_data/main.go index 424496c..f0496c1 100644 --- a/examples/json_data/main.go +++ b/examples/json_data/main.go @@ -13,7 +13,7 @@ func main() { app := router.NewApp() // Define a route that responds to a GET request at "/json". - app.Route("GET", "/json", func(r *req.Request, w *req.Response) { + app.Get("/json", func(r *req.Request, w *req.Response) { data := map[string]string{ "message": "Hello, JSON", } diff --git a/examples/simple_route/main.go b/examples/simple_route/main.go index 420aeef..914a3b3 100644 --- a/examples/simple_route/main.go +++ b/examples/simple_route/main.go @@ -13,7 +13,7 @@ func main() { app := router.NewApp() // Define a route that responds to a GET request at "/hello". - app.Route("GET", "/hello", func(r *req.Request, w *req.Response) { + app.Get("/hello", func(r *req.Request, w *req.Response) { w.Send("Hello, World!") }) diff --git a/router/router.go b/router/router.go index a2f9320..122d0d2 100644 --- a/router/router.go +++ b/router/router.go @@ -47,6 +47,39 @@ func (a *App) Route(method string, path string, handler func(req *req.Request, r a.routes[path][method] = h } +func (a *App) Get(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodGet, path, handler) +} + +func (a *App) Post(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodPost, path, handler) +} + +func (a *App) Put(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodPut, path, handler) +} + +func (a *App) Delete(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodDelete, path, handler) +} + +func (a *App) Patch(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodPatch, path, handler) +} +func (a *App) Options(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodOptions, path, handler) +} +func (a *App) Head(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodHead, path, handler) +} + +func (a *App) Connect(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodConnect, path, handler) +} +func (a *App) Trace(path string, handler func(req *req.Request, res *req.Response)) { + a.Route(http.MethodTrace, path, handler) +} + func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { if handlers, exists := a.routes[r.URL.Path]; exists { if handler, methodExists := handlers[r.Method]; methodExists {