Skip to content

Commit

Permalink
[ENHANCEMENT] Add the possibility to inject pre-middleware for the HT…
Browse files Browse the repository at this point in the history
…TP server (#224)

Signed-off-by: Augustin Husson <[email protected]>
  • Loading branch information
Nexucis authored Aug 19, 2024
1 parent 4b64752 commit bdf5def
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Builder struct {
apis []Register
overrideMiddleware bool
mdws []echo.MiddlewareFunc
preMDWs []echo.MiddlewareFunc
gzipSkipper middleware.Skipper
activatePprof bool
}
Expand All @@ -97,6 +98,13 @@ func NewBuilder(addr string) *Builder {
}
}

// PreMiddleware is adding the provided middleware into the Builder.
// Each mdw added here, will be executed before the router.
func (b *Builder) PreMiddleware(mdw echo.MiddlewareFunc) *Builder {
b.preMDWs = append(b.preMDWs, mdw)
return b
}

// Middleware is adding the provided middleware into the Builder
// Order matters, add the middleware in the order you would like to see them started.
func (b *Builder) Middleware(mdw echo.MiddlewareFunc) *Builder {
Expand Down Expand Up @@ -205,6 +213,7 @@ func (b *Builder) build() (*server, error) {
apis: b.apis,
e: e,
mdws: b.mdws,
preMDWs: b.preMDWs,
shutdownTimeout: 30 * time.Second,
activatePprof: b.activatePprof,
}, nil
Expand All @@ -216,6 +225,7 @@ type server struct {
apis []Register
e *echo.Echo
mdws []echo.MiddlewareFunc
preMDWs []echo.MiddlewareFunc
shutdownTimeout time.Duration
activatePprof bool
}
Expand All @@ -228,6 +238,9 @@ func (s *server) Initialize() error {
// init global middleware
// Remove trailing slash middleware a trailing slash from the request URI
s.e.Pre(middleware.RemoveTrailingSlash())
for _, p := range s.preMDWs {
s.e.Pre(p)
}
for _, mdw := range s.mdws {
s.e.Use(mdw)
}
Expand Down

0 comments on commit bdf5def

Please sign in to comment.