From ac8ba898650057a437c3aee5c159f77138a00f91 Mon Sep 17 00:00:00 2001 From: Zeni Kim Date: Sun, 15 Sep 2024 19:00:38 -0500 Subject: [PATCH] migration --- handler.go => controller.go | 2 +- core.go | 22 +++++++++++----------- router.go | 30 +++++++++++++++--------------- router_test.go | 16 ++++++++-------- 4 files changed, 35 insertions(+), 35 deletions(-) rename handler.go => controller.go (85%) diff --git a/handler.go b/controller.go similarity index 85% rename from handler.go rename to controller.go index c3ec69f..71b44b1 100644 --- a/handler.go +++ b/controller.go @@ -5,4 +5,4 @@ package core -type Handler func(c *Context) *Response +type Controller func(c *Context) *Response diff --git a/core.go b/core.go index 51212d4..f647d2c 100644 --- a/core.go +++ b/core.go @@ -138,25 +138,25 @@ func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httpr for _, route := range routes { switch route.Method { case GET: - router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) case POST: - router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) case DELETE: - router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) case PATCH: - router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) case PUT: - router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) case OPTIONS: - router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) case HEAD: - router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) + router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) } } return router } -func (app *App) makeHTTPRouterHandlerFunc(h Handler, ms []Middleware) httprouter.Handle { +func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Middleware) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { ctx := &Context{ Request: &Request{ @@ -285,7 +285,7 @@ func (app *App) Next(c *Context) { if ok { f(c) } else { - ff, ok := n.(Handler) + ff, ok := n.(Controller) if ok { ff(c) } @@ -328,7 +328,7 @@ func (cn *chain) execute(ctx *Context) { if ok { f(ctx) } else { - ff, ok := i.(Handler) + ff, ok := i.(Controller) if ok { ff(ctx) } @@ -336,7 +336,7 @@ func (cn *chain) execute(ctx *Context) { } } -func (app *App) combHandlers(h Handler, mw []Middleware) []interface{} { +func (app *App) combHandlers(h Controller, mw []Middleware) []interface{} { var rev []interface{} for _, k := range mw { rev = append(rev, k) diff --git a/router.go b/router.go index 5691540..4e262f0 100644 --- a/router.go +++ b/router.go @@ -8,7 +8,7 @@ package core type Route struct { Method string Path string - Handler Handler + Controller Controller Middlewares []Middleware } @@ -29,71 +29,71 @@ func ResolveRouter() *Router { return router } -func (r *Router) Get(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Get(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: GET, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r } -func (r *Router) Post(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Post(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: POST, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r } -func (r *Router) Delete(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Delete(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: DELETE, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r } -func (r *Router) Patch(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Patch(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: PATCH, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r } -func (r *Router) Put(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Put(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: PUT, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r } -func (r *Router) Options(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Options(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: OPTIONS, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r } -func (r *Router) Head(path string, handler Handler, middlewares ...Middleware) *Router { +func (r *Router) Head(path string, controller Controller, middlewares ...Middleware) *Router { r.Routes = append(r.Routes, Route{ Method: HEAD, Path: path, - Handler: handler, + Controller: controller, Middlewares: middlewares, }) return r diff --git a/router_test.go b/router_test.go index afcdf4c..12e7775 100644 --- a/router_test.go +++ b/router_test.go @@ -27,7 +27,7 @@ func TestResolveRouter(t *testing.T) { func TestGetRequest(t *testing.T) { r := NewRouter() - handler := Handler(func(c *Context) *Response { + handler := Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }) @@ -41,7 +41,7 @@ func TestGetRequest(t *testing.T) { func TestPostRequest(t *testing.T) { r := NewRouter() - handler := Handler(func(c *Context) *Response { + handler := Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }) @@ -55,7 +55,7 @@ func TestPostRequest(t *testing.T) { func TestDeleteRequest(t *testing.T) { r := NewRouter() - handler := Handler(func(c *Context) *Response { + handler := Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }) @@ -69,7 +69,7 @@ func TestDeleteRequest(t *testing.T) { func TestPutRequest(t *testing.T) { r := NewRouter() - handler := Handler(func(c *Context) *Response { + handler := Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }) @@ -83,7 +83,7 @@ func TestPutRequest(t *testing.T) { func TestOptionsRequest(t *testing.T) { r := NewRouter() - handler := Handler(func(c *Context) *Response { + handler := Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }) @@ -97,7 +97,7 @@ func TestOptionsRequest(t *testing.T) { func TestHeadRequest(t *testing.T) { r := NewRouter() - handler := Handler(func(c *Context) *Response { + handler := Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }) @@ -111,11 +111,11 @@ func TestHeadRequest(t *testing.T) { func TestAddMultipleRoutes(t *testing.T) { r := NewRouter() - r.Get("/", Handler(func(c *Context) *Response { + r.Get("/", Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil })) - r.Post("/", Handler(func(c *Context) *Response { + r.Post("/", Controller(func(c *Context) *Response { c.GetLogger().Info(TEST_STR) return nil }))