1
0
Fork 0
forked from goffee/core

migration

This commit is contained in:
Zeni Kim 2024-09-15 19:00:38 -05:00
parent 7f38826b9c
commit ac8ba89865
4 changed files with 35 additions and 35 deletions

View file

@ -5,4 +5,4 @@
package core
type Handler func(c *Context) *Response
type Controller func(c *Context) *Response

22
core.go
View file

@ -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)

View file

@ -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

View file

@ -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
}))