migration
This commit is contained in:
parent
ac8ba89865
commit
c6006861cb
6 changed files with 100 additions and 100 deletions
40
core.go
40
core.go
|
@ -42,18 +42,18 @@ type configContainer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
t int // for trancking middlewares
|
t int // for trancking hooks
|
||||||
chain *chain
|
chain *chain
|
||||||
middlewares *Middlewares
|
hooks *Hooks
|
||||||
Config *configContainer
|
Config *configContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
var app *App
|
var app *App
|
||||||
|
|
||||||
func New() *App {
|
func New() *App {
|
||||||
app = &App{
|
app = &App{
|
||||||
chain: &chain{},
|
chain: &chain{},
|
||||||
middlewares: NewMiddlewares(),
|
hooks: NewHooks(),
|
||||||
Config: &configContainer{
|
Config: &configContainer{
|
||||||
Request: requestC,
|
Request: requestC,
|
||||||
},
|
},
|
||||||
|
@ -138,25 +138,25 @@ func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httpr
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
switch route.Method {
|
switch route.Method {
|
||||||
case GET:
|
case GET:
|
||||||
router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
case POST:
|
case POST:
|
||||||
router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
case DELETE:
|
case DELETE:
|
||||||
router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
case PATCH:
|
case PATCH:
|
||||||
router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
case PUT:
|
case PUT:
|
||||||
router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
case OPTIONS:
|
case OPTIONS:
|
||||||
router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
case HEAD:
|
case HEAD:
|
||||||
router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
|
router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return router
|
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) {
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
ctx := &Context{
|
ctx := &Context{
|
||||||
Request: &Request{
|
Request: &Request{
|
||||||
|
@ -273,15 +273,15 @@ var panicHandler = func(w http.ResponseWriter, r *http.Request, e interface{}) {
|
||||||
w.Write([]byte(res))
|
w.Write([]byte(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
func UseMiddleware(mw Middleware) {
|
func UseHook(mw Hook) {
|
||||||
ResolveMiddlewares().Attach(mw)
|
ResolveHooks().Attach(mw)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Next(c *Context) {
|
func (app *App) Next(c *Context) {
|
||||||
app.t = app.t + 1
|
app.t = app.t + 1
|
||||||
n := app.chain.getByIndex(app.t)
|
n := app.chain.getByIndex(app.t)
|
||||||
if n != nil {
|
if n != nil {
|
||||||
f, ok := n.(Middleware)
|
f, ok := n.(Hook)
|
||||||
if ok {
|
if ok {
|
||||||
f(c)
|
f(c)
|
||||||
} else {
|
} else {
|
||||||
|
@ -312,7 +312,7 @@ func (c *chain) getByIndex(i int) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) prepareChain(hs []interface{}) {
|
func (app *App) prepareChain(hs []interface{}) {
|
||||||
mw := app.middlewares.GetMiddlewares()
|
mw := app.hooks.GetHooks()
|
||||||
for _, v := range mw {
|
for _, v := range mw {
|
||||||
app.chain.nodes = append(app.chain.nodes, v)
|
app.chain.nodes = append(app.chain.nodes, v)
|
||||||
}
|
}
|
||||||
|
@ -324,7 +324,7 @@ func (app *App) prepareChain(hs []interface{}) {
|
||||||
func (cn *chain) execute(ctx *Context) {
|
func (cn *chain) execute(ctx *Context) {
|
||||||
i := cn.getByIndex(0)
|
i := cn.getByIndex(0)
|
||||||
if i != nil {
|
if i != nil {
|
||||||
f, ok := i.(Middleware)
|
f, ok := i.(Hook)
|
||||||
if ok {
|
if ok {
|
||||||
f(ctx)
|
f(ctx)
|
||||||
} else {
|
} 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{}
|
var rev []interface{}
|
||||||
for _, k := range mw {
|
for _, k := range mw {
|
||||||
rev = append(rev, k)
|
rev = append(rev, k)
|
||||||
|
|
|
@ -5,4 +5,4 @@
|
||||||
|
|
||||||
package core
|
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
|
|
||||||
}
|
|
78
router.go
78
router.go
|
@ -6,10 +6,10 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Method string
|
Method string
|
||||||
Path string
|
Path string
|
||||||
Controller Controller
|
Controller Controller
|
||||||
Middlewares []Middleware
|
Hooks []Hook
|
||||||
}
|
}
|
||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
|
@ -29,72 +29,72 @@ func ResolveRouter() *Router {
|
||||||
return 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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: GET,
|
Method: GET,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: POST,
|
Method: POST,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: DELETE,
|
Method: DELETE,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: PATCH,
|
Method: PATCH,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: PUT,
|
Method: PUT,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: OPTIONS,
|
Method: OPTIONS,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
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{
|
r.Routes = append(r.Routes, Route{
|
||||||
Method: HEAD,
|
Method: HEAD,
|
||||||
Path: path,
|
Path: path,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Middlewares: middlewares,
|
Hooks: hooks,
|
||||||
})
|
})
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue