Skip to content

Commit

Permalink
fix: prefix problem
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 4, 2023
1 parent 3aac9f1 commit e88b351
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
39 changes: 25 additions & 14 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,51 @@ func (r *Group) Middleware(middlewares ...httpcontract.Middleware) route.Router
}

func (r *Group) Any(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).All(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).All(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Get(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Get(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Get(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Post(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Post(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Post(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Delete(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Delete(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Delete(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Patch(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Patch(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Patch(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Put(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Put(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Put(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Options(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Options(r.getPath(relativePath), r.getMiddlewares(handler)...)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Options(relativePath, r.getMiddlewares(handler)...)
r.clearMiddlewares()
}

func (r *Group) Resource(relativePath string, controller httpcontract.ResourceController) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Get(r.getPath(relativePath), r.getMiddlewares(controller.Index)...)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Post(r.getPath(relativePath), r.getMiddlewares(controller.Store)...)
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)...)
Expand All @@ -103,12 +111,14 @@ func (r *Group) Resource(relativePath string, controller httpcontract.ResourceCo
}

func (r *Group) Static(relativePath, root string) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Static(r.getPath(relativePath), root)
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Static(relativePath, root)
r.clearMiddlewares()
}

func (r *Group) StaticFile(relativePath, filePath string) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(r.getPath(relativePath), func(c *fiber.Ctx) error {
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).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)
Expand All @@ -119,7 +129,8 @@ func (r *Group) StaticFile(relativePath, filePath string) {
}

func (r *Group) StaticFS(relativePath string, fs http.FileSystem) {
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(r.getPath(relativePath), filesystem.New(filesystem.Config{
relativePath = r.getPath(relativePath)
r.instance.Use(r.getGlobalMiddlewaresWithPath(relativePath)...).Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, filesystem.New(filesystem.Config{
Root: fs,
}))
r.clearMiddlewares()
Expand All @@ -146,7 +157,7 @@ func (r *Group) getPath(relativePath string) string {

func (r *Group) getMiddlewaresWithPath(relativePath string, handler httpcontract.HandlerFunc) []any {
var handlers []any
handlers = append(handlers, r.getPath(relativePath))
handlers = append(handlers, relativePath)
middlewares := r.getMiddlewares(handler)

// Fiber will panic if no middleware is provided, So we add a dummy middleware
Expand All @@ -165,7 +176,7 @@ func (r *Group) getMiddlewaresWithPath(relativePath string, handler httpcontract

func (r *Group) getGlobalMiddlewaresWithPath(relativePath string) []any {
var handlers []any
handlers = append(handlers, r.getPath(relativePath))
handlers = append(handlers, relativePath)
handlers = append(handlers, r.globalMiddlewares...)

return handlers
Expand Down
70 changes: 35 additions & 35 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{"*"}).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()
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)

resource := resourceController{}
fiber.GlobalMiddleware(func(ctx contractshttp.Context) {
Expand All @@ -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{"*"}).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()
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)

resource := resourceController{}
fiber.GlobalMiddleware(func(ctx contractshttp.Context) {
Expand All @@ -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{"*"}).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()
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)

resource := resourceController{}
fiber.GlobalMiddleware(func(ctx contractshttp.Context) {
Expand All @@ -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{"*"}).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()
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)

resource := resourceController{}
fiber.GlobalMiddleware(func(ctx contractshttp.Context) {
Expand All @@ -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{"*"}).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()
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)

resource := resourceController{}
fiber.GlobalMiddleware(func(ctx contractshttp.Context) {
Expand Down

0 comments on commit e88b351

Please sign in to comment.