diff --git a/.goreleaser.yaml b/.goreleaser.yaml deleted file mode 100644 index 2bb7412..0000000 --- a/.goreleaser.yaml +++ /dev/null @@ -1,2 +0,0 @@ -builds: - - skip: true diff --git a/context_request.go b/context_request.go index 225cf89..109f434 100644 --- a/context_request.go +++ b/context_request.go @@ -155,6 +155,13 @@ func (r *ContextRequest) Method() string { func (r *ContextRequest) Next() { if err := r.instance.Next(); err != nil { + var fiberErr *fiber.Error + if errors.As(err, &fiberErr) { + if err := r.instance.Status(fiberErr.Code).SendString(fiberErr.Message); err == nil { + return + } + } + panic(err) } } diff --git a/group.go b/group.go index 8d02f20..139ffd0 100644 --- a/group.go +++ b/group.go @@ -17,18 +17,16 @@ type Group struct { instance *fiber.App originPrefix string prefix string - globalMiddlewares []any originMiddlewares []httpcontract.Middleware middlewares []httpcontract.Middleware lastMiddlewares []httpcontract.Middleware } -func NewGroup(config config.Config, instance *fiber.App, prefix string, globalMiddlewares []any, originMiddlewares []httpcontract.Middleware, lastMiddlewares []httpcontract.Middleware) route.Router { +func NewGroup(config config.Config, instance *fiber.App, prefix string, originMiddlewares []httpcontract.Middleware, lastMiddlewares []httpcontract.Middleware) route.Router { return &Group{ config: config, instance: instance, originPrefix: prefix, - globalMiddlewares: globalMiddlewares, originMiddlewares: originMiddlewares, lastMiddlewares: lastMiddlewares, } @@ -42,7 +40,7 @@ func (r *Group) Group(handler route.GroupFunc) { prefix := pathToFiberPath(r.originPrefix + "/" + r.prefix) r.prefix = "" - handler(NewGroup(r.config, r.instance, prefix, r.globalMiddlewares, middlewares, r.lastMiddlewares)) + handler(NewGroup(r.config, r.instance, prefix, middlewares, r.lastMiddlewares)) } func (r *Group) Prefix(addr string) route.Router { @@ -58,67 +56,60 @@ func (r *Group) Middleware(middlewares ...httpcontract.Middleware) route.Router } func (r *Group) Any(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).All(relativePath, r.getMiddlewares(handler)...) + r.instance.All(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Get(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Get(relativePath, r.getMiddlewares(handler)...) + r.instance.Get(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Post(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Post(relativePath, r.getMiddlewares(handler)...) + r.instance.Post(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Delete(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Delete(relativePath, r.getMiddlewares(handler)...) + r.instance.Delete(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Patch(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Patch(relativePath, r.getMiddlewares(handler)...) + r.instance.Patch(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Put(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Put(relativePath, r.getMiddlewares(handler)...) + r.instance.Put(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Options(relativePath string, handler httpcontract.HandlerFunc) { - relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Options(relativePath, r.getMiddlewares(handler)...) + r.instance.Options(r.getPath(relativePath), r.getMiddlewares(handler)...) r.clearMiddlewares() } func (r *Group) Resource(relativePath string, controller httpcontract.ResourceController) { relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Get(relativePath, r.getMiddlewares(controller.Index)...) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Post(relativePath, r.getMiddlewares(controller.Store)...) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Get(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Show)...) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Put(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Update)...) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Patch(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Update)...) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Delete(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Destroy)...) + r.instance.Get(relativePath, r.getMiddlewares(controller.Index)...) + r.instance.Post(relativePath, r.getMiddlewares(controller.Store)...) + r.instance.Get(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Show)...) + r.instance.Put(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Update)...) + r.instance.Patch(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Update)...) + r.instance.Delete(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Destroy)...) r.clearMiddlewares() } func (r *Group) Static(relativePath, root string) { relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Static(relativePath, root) + r.instance.Use(r.getMiddlewaresWithPath(r.getPath(relativePath), nil)...).Static(relativePath, root) r.clearMiddlewares() } func (r *Group) StaticFile(relativePath, filePath string) { relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, func(c *fiber.Ctx) error { + r.instance.Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, func(c *fiber.Ctx) error { dir, file := filepath.Split(filePath) escapedFile := url.PathEscape(file) escapedPath := filepath.Join(dir, escapedFile) @@ -130,7 +121,7 @@ func (r *Group) StaticFile(relativePath, filePath string) { func (r *Group) StaticFS(relativePath string, fs http.FileSystem) { relativePath = r.getPath(relativePath) - r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, filesystem.New(filesystem.Config{ + r.instance.Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, filesystem.New(filesystem.Config{ Root: fs, })) r.clearMiddlewares() @@ -174,14 +165,6 @@ func (r *Group) getMiddlewaresWithPath(relativePath string, handler httpcontract return handlers } -func (r *Group) getGlobalMiddlewaresWithPath(relativePath string) []any { - var handlers []any - handlers = append(handlers, relativePath) - handlers = append(handlers, r.globalMiddlewares...) - - return handlers -} - func (r *Group) clearMiddlewares() { r.middlewares = []httpcontract.Middleware{} } diff --git a/group_test.go b/group_test.go index bbb6134..a40a039 100644 --- a/group_test.go +++ b/group_test.go @@ -263,13 +263,13 @@ func TestGroup(t *testing.T) { setup: func(req *http.Request) { mockConfig.On("GetBool", "app.debug", false).Return(true).Twice() mockConfig.On("GetString", "app.timezone", "UTC").Return("UTC").Once() - mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Times(3) - mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Times(3) - mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Times(3) - mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Times(3) - mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Times(3) - mockConfig.On("GetInt", "cors.max_age").Return(0).Times(3) - mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Times(3) + mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Once() + mockConfig.On("GetInt", "cors.max_age").Return(0).Once() + mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Once() resource := resourceController{} fiber.GlobalMiddleware(func(ctx contractshttp.Context) { @@ -288,13 +288,13 @@ func TestGroup(t *testing.T) { setup: func(req *http.Request) { mockConfig.On("GetBool", "app.debug", false).Return(true).Twice() mockConfig.On("GetString", "app.timezone", "UTC").Return("UTC").Once() - mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Times(2) - mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Times(2) - mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Times(2) - mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Times(2) - mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Times(2) - mockConfig.On("GetInt", "cors.max_age").Return(0).Times(2) - mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Times(2) + mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Once() + mockConfig.On("GetInt", "cors.max_age").Return(0).Once() + mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Once() resource := resourceController{} fiber.GlobalMiddleware(func(ctx contractshttp.Context) { @@ -313,13 +313,13 @@ func TestGroup(t *testing.T) { setup: func(req *http.Request) { mockConfig.On("GetBool", "app.debug", false).Return(true).Twice() mockConfig.On("GetString", "app.timezone", "UTC").Return("UTC").Once() - mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Times(4) - mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Times(4) - mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Times(4) - mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Times(4) - mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Times(4) - mockConfig.On("GetInt", "cors.max_age").Return(0).Times(4) - mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Times(4) + mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Once() + mockConfig.On("GetInt", "cors.max_age").Return(0).Once() + mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Once() resource := resourceController{} fiber.GlobalMiddleware(func(ctx contractshttp.Context) { @@ -338,13 +338,13 @@ func TestGroup(t *testing.T) { setup: func(req *http.Request) { mockConfig.On("GetBool", "app.debug", false).Return(true).Twice() mockConfig.On("GetString", "app.timezone", "UTC").Return("UTC").Once() - mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Times(5) - mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Times(5) - mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Times(5) - mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Times(5) - mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Times(5) - mockConfig.On("GetInt", "cors.max_age").Return(0).Times(5) - mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Times(5) + mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Once() + mockConfig.On("GetInt", "cors.max_age").Return(0).Once() + mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Once() resource := resourceController{} fiber.GlobalMiddleware(func(ctx contractshttp.Context) { @@ -363,13 +363,13 @@ func TestGroup(t *testing.T) { setup: func(req *http.Request) { mockConfig.On("GetBool", "app.debug", false).Return(true).Twice() mockConfig.On("GetString", "app.timezone", "UTC").Return("UTC").Once() - mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Times(6) - mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Times(6) - mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Times(6) - mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Times(6) - mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Times(6) - mockConfig.On("GetInt", "cors.max_age").Return(0).Times(6) - mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Times(6) + mockConfig.On("Get", "cors.paths").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_methods").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_origins").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.allowed_headers").Return([]string{"*"}).Once() + mockConfig.On("Get", "cors.exposed_headers").Return([]string{"*"}).Once() + mockConfig.On("GetInt", "cors.max_age").Return(0).Once() + mockConfig.On("GetBool", "cors.supports_credentials").Return(false).Once() resource := resourceController{} fiber.GlobalMiddleware(func(ctx contractshttp.Context) { diff --git a/route.go b/route.go index 38fa75b..3d9bb26 100644 --- a/route.go +++ b/route.go @@ -68,9 +68,6 @@ func NewRoute(config config.Config, parameters map[string]any) (*Route, error) { config, app, "", - []any{func(c *fiber.Ctx) error { - return c.Next() - }}, []httpcontract.Middleware{}, []httpcontract.Middleware{}, ), @@ -111,11 +108,12 @@ func (r *Route) GlobalMiddleware(middlewares ...httpcontract.Middleware) { tempMiddlewares = append(tempMiddlewares, middleware) } + r.instance.Use(tempMiddlewares...) + r.Router = NewGroup( r.config, r.instance, "", - tempMiddlewares, []httpcontract.Middleware{}, []httpcontract.Middleware{ResponseMiddleware()}, )