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 {
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue