From c6006861cb29beb6614af7a1e6c189d4e5983548 Mon Sep 17 00:00:00 2001 From: Zeni Kim Date: Sun, 15 Sep 2024 19:19:54 -0500 Subject: [PATCH] migration --- core.go | 40 +++++++------- middleware.go => hook.go | 2 +- hooks.go | 40 ++++++++++++++ middlewares_test.go => hooks_test.go | 0 middlewares.go | 40 -------------- router.go | 78 ++++++++++++++-------------- 6 files changed, 100 insertions(+), 100 deletions(-) rename middleware.go => hook.go (88%) create mode 100644 hooks.go rename middlewares_test.go => hooks_test.go (100%) delete mode 100644 middlewares.go diff --git a/core.go b/core.go index f647d2c..4409708 100644 --- a/core.go +++ b/core.go @@ -42,18 +42,18 @@ type configContainer struct { } type App struct { - t int // for trancking middlewares - chain *chain - middlewares *Middlewares - Config *configContainer + t int // for trancking hooks + chain *chain + hooks *Hooks + Config *configContainer } var app *App func New() *App { app = &App{ - chain: &chain{}, - middlewares: NewMiddlewares(), + chain: &chain{}, + hooks: NewHooks(), Config: &configContainer{ Request: requestC, }, @@ -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.Controller, route.Middlewares)) + router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) case POST: - router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) + router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) case DELETE: - router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) + router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) case PATCH: - router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) + router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) case PUT: - router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) + router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) case OPTIONS: - router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) + router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) case HEAD: - router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares)) + router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) } } return router } -func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Middleware) httprouter.Handle { +func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { ctx := &Context{ Request: &Request{ @@ -273,15 +273,15 @@ var panicHandler = func(w http.ResponseWriter, r *http.Request, e interface{}) { w.Write([]byte(res)) } -func UseMiddleware(mw Middleware) { - ResolveMiddlewares().Attach(mw) +func UseHook(mw Hook) { + ResolveHooks().Attach(mw) } func (app *App) Next(c *Context) { app.t = app.t + 1 n := app.chain.getByIndex(app.t) if n != nil { - f, ok := n.(Middleware) + f, ok := n.(Hook) if ok { f(c) } else { @@ -312,7 +312,7 @@ func (c *chain) getByIndex(i int) interface{} { } func (app *App) prepareChain(hs []interface{}) { - mw := app.middlewares.GetMiddlewares() + mw := app.hooks.GetHooks() for _, v := range mw { app.chain.nodes = append(app.chain.nodes, v) } @@ -324,7 +324,7 @@ func (app *App) prepareChain(hs []interface{}) { func (cn *chain) execute(ctx *Context) { i := cn.getByIndex(0) if i != nil { - f, ok := i.(Middleware) + f, ok := i.(Hook) if ok { f(ctx) } else { @@ -336,7 +336,7 @@ func (cn *chain) execute(ctx *Context) { } } -func (app *App) combHandlers(h Controller, mw []Middleware) []interface{} { +func (app *App) combHandlers(h Controller, mw []Hook) []interface{} { var rev []interface{} for _, k := range mw { rev = append(rev, k) diff --git a/middleware.go b/hook.go similarity index 88% rename from middleware.go rename to hook.go index 2ff567c..1fbd90e 100644 --- a/middleware.go +++ b/hook.go @@ -5,4 +5,4 @@ package core -type Middleware func(c *Context) +type Hook func(c *Context) diff --git a/hooks.go b/hooks.go new file mode 100644 index 0000000..92798da --- /dev/null +++ b/hooks.go @@ -0,0 +1,40 @@ +// Copyright 2021 Harran Ali . All rights reserved. +// Copyright (c) 2024 Zeni Kim +// Use of this source code is governed by MIT-style +// license that can be found in the LICENSE file. + +package core + +type Hooks struct { + hooks []Hook +} + +var m *Hooks + +func NewHooks() *Hooks { + m = &Hooks{} + return m +} + +func ResolveHooks() *Hooks { + return m +} + +func (m *Hooks) Attach(mw Hook) *Hooks { + m.hooks = append(m.hooks, mw) + + return m +} + +func (m *Hooks) GetHooks() []Hook { + return m.hooks +} + +func (m *Hooks) getByIndex(i int) Hook { + for k := range m.hooks { + if k == i { + return m.hooks[i] + } + } + return nil +} diff --git a/middlewares_test.go b/hooks_test.go similarity index 100% rename from middlewares_test.go rename to hooks_test.go diff --git a/middlewares.go b/middlewares.go deleted file mode 100644 index 1d009fb..0000000 --- a/middlewares.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2021 Harran Ali . All rights reserved. -// Copyright (c) 2024 Zeni Kim -// Use of this source code is governed by MIT-style -// license that can be found in the LICENSE file. - -package core - -type Middlewares struct { - middlewares []Middleware -} - -var m *Middlewares - -func NewMiddlewares() *Middlewares { - m = &Middlewares{} - return m -} - -func ResolveMiddlewares() *Middlewares { - return m -} - -func (m *Middlewares) Attach(mw Middleware) *Middlewares { - m.middlewares = append(m.middlewares, mw) - - return m -} - -func (m *Middlewares) GetMiddlewares() []Middleware { - return m.middlewares -} - -func (m *Middlewares) getByIndex(i int) Middleware { - for k := range m.middlewares { - if k == i { - return m.middlewares[i] - } - } - return nil -} diff --git a/router.go b/router.go index 4e262f0..24141b4 100644 --- a/router.go +++ b/router.go @@ -6,10 +6,10 @@ package core type Route struct { - Method string - Path string - Controller Controller - Middlewares []Middleware + Method string + Path string + Controller Controller + Hooks []Hook } type Router struct { @@ -29,72 +29,72 @@ func ResolveRouter() *Router { return router } -func (r *Router) Get(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: GET, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: GET, + Path: path, + Controller: controller, + Hooks: hooks, }) return r } -func (r *Router) Post(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: POST, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: POST, + Path: path, + Controller: controller, + Hooks: hooks, }) return r } -func (r *Router) Delete(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: DELETE, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: DELETE, + Path: path, + Controller: controller, + Hooks: hooks, }) return r } -func (r *Router) Patch(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: PATCH, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: PATCH, + Path: path, + Controller: controller, + Hooks: hooks, }) return r } -func (r *Router) Put(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: PUT, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: PUT, + Path: path, + Controller: controller, + Hooks: hooks, }) return r } -func (r *Router) Options(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: OPTIONS, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: OPTIONS, + Path: path, + Controller: controller, + Hooks: hooks, }) return r } -func (r *Router) Head(path string, controller Controller, middlewares ...Middleware) *Router { +func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ - Method: HEAD, - Path: path, - Controller: controller, - Middlewares: middlewares, + Method: HEAD, + Path: path, + Controller: controller, + Hooks: hooks, }) return r }