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 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 { for _, route := range routes {
switch route.Method { switch route.Method {
case GET: case GET:
router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.GET(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
case POST: case POST:
router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.POST(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
case DELETE: case DELETE:
router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.DELETE(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
case PATCH: case PATCH:
router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.PATCH(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
case PUT: case PUT:
router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.PUT(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
case OPTIONS: case OPTIONS:
router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.OPTIONS(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
case HEAD: case HEAD:
router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Handler, route.Middlewares)) router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Middlewares))
} }
} }
return router 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) { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := &Context{ ctx := &Context{
Request: &Request{ Request: &Request{
@ -285,7 +285,7 @@ func (app *App) Next(c *Context) {
if ok { if ok {
f(c) f(c)
} else { } else {
ff, ok := n.(Handler) ff, ok := n.(Controller)
if ok { if ok {
ff(c) ff(c)
} }
@ -328,7 +328,7 @@ func (cn *chain) execute(ctx *Context) {
if ok { if ok {
f(ctx) f(ctx)
} else { } else {
ff, ok := i.(Handler) ff, ok := i.(Controller)
if ok { if ok {
ff(ctx) 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{} var rev []interface{}
for _, k := range mw { for _, k := range mw {
rev = append(rev, k) rev = append(rev, k)

View file

@ -8,7 +8,7 @@ package core
type Route struct { type Route struct {
Method string Method string
Path string Path string
Handler Handler Controller Controller
Middlewares []Middleware Middlewares []Middleware
} }
@ -29,71 +29,71 @@ func ResolveRouter() *Router {
return 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{ r.Routes = append(r.Routes, Route{
Method: GET, Method: GET,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r 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{ r.Routes = append(r.Routes, Route{
Method: POST, Method: POST,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r 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{ r.Routes = append(r.Routes, Route{
Method: DELETE, Method: DELETE,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r 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{ r.Routes = append(r.Routes, Route{
Method: PATCH, Method: PATCH,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r 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{ r.Routes = append(r.Routes, Route{
Method: PUT, Method: PUT,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r 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{ r.Routes = append(r.Routes, Route{
Method: OPTIONS, Method: OPTIONS,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r 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{ r.Routes = append(r.Routes, Route{
Method: HEAD, Method: HEAD,
Path: path, Path: path,
Handler: handler, Controller: controller,
Middlewares: middlewares, Middlewares: middlewares,
}) })
return r return r

View file

@ -27,7 +27,7 @@ func TestResolveRouter(t *testing.T) {
func TestGetRequest(t *testing.T) { func TestGetRequest(t *testing.T) {
r := NewRouter() r := NewRouter()
handler := Handler(func(c *Context) *Response { handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
}) })
@ -41,7 +41,7 @@ func TestGetRequest(t *testing.T) {
func TestPostRequest(t *testing.T) { func TestPostRequest(t *testing.T) {
r := NewRouter() r := NewRouter()
handler := Handler(func(c *Context) *Response { handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
}) })
@ -55,7 +55,7 @@ func TestPostRequest(t *testing.T) {
func TestDeleteRequest(t *testing.T) { func TestDeleteRequest(t *testing.T) {
r := NewRouter() r := NewRouter()
handler := Handler(func(c *Context) *Response { handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
}) })
@ -69,7 +69,7 @@ func TestDeleteRequest(t *testing.T) {
func TestPutRequest(t *testing.T) { func TestPutRequest(t *testing.T) {
r := NewRouter() r := NewRouter()
handler := Handler(func(c *Context) *Response { handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
}) })
@ -83,7 +83,7 @@ func TestPutRequest(t *testing.T) {
func TestOptionsRequest(t *testing.T) { func TestOptionsRequest(t *testing.T) {
r := NewRouter() r := NewRouter()
handler := Handler(func(c *Context) *Response { handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
}) })
@ -97,7 +97,7 @@ func TestOptionsRequest(t *testing.T) {
func TestHeadRequest(t *testing.T) { func TestHeadRequest(t *testing.T) {
r := NewRouter() r := NewRouter()
handler := Handler(func(c *Context) *Response { handler := Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
}) })
@ -111,11 +111,11 @@ func TestHeadRequest(t *testing.T) {
func TestAddMultipleRoutes(t *testing.T) { func TestAddMultipleRoutes(t *testing.T) {
r := NewRouter() r := NewRouter()
r.Get("/", Handler(func(c *Context) *Response { r.Get("/", Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
})) }))
r.Post("/", Handler(func(c *Context) *Response { r.Post("/", Controller(func(c *Context) *Response {
c.GetLogger().Info(TEST_STR) c.GetLogger().Info(TEST_STR)
return nil return nil
})) }))