forked from goffee/core
migration
This commit is contained in:
parent
ac8ba89865
commit
c6006861cb
6 changed files with 100 additions and 100 deletions
34
core.go
34
core.go
|
@ -42,9 +42,9 @@ type configContainer struct {
|
|||
}
|
||||
|
||||
type App struct {
|
||||
t int // for trancking middlewares
|
||||
t int // for trancking hooks
|
||||
chain *chain
|
||||
middlewares *Middlewares
|
||||
hooks *Hooks
|
||||
Config *configContainer
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,7 @@ var app *App
|
|||
func New() *App {
|
||||
app = &App{
|
||||
chain: &chain{},
|
||||
middlewares: NewMiddlewares(),
|
||||
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)
|
||||
|
|
|
@ -5,4 +5,4 @@
|
|||
|
||||
package core
|
||||
|
||||
type Middleware func(c *Context)
|
||||
type Hook func(c *Context)
|
40
hooks.go
Normal file
40
hooks.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2021 Harran Ali <harran.m@gmail.com>. All rights reserved.
|
||||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// 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
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
// Copyright 2021 Harran Ali <harran.m@gmail.com>. All rights reserved.
|
||||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// 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
|
||||
}
|
30
router.go
30
router.go
|
@ -9,7 +9,7 @@ type Route struct {
|
|||
Method string
|
||||
Path string
|
||||
Controller Controller
|
||||
Middlewares []Middleware
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
Hooks: hooks,
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue