change the behavior of RunWithMiddlewares (#2063)

This commit is contained in:
Mohammed Nafees
2025-07-30 14:04:44 +02:00
committed by GitHub
parent 682b535cae
commit 1b2a2bf566
3 changed files with 134 additions and 496 deletions

View File

@@ -146,20 +146,17 @@ output-options:
"strict/strict-echo.tmpl": >-
type StrictHandlerFunc func(ctx echo.Context, args interface{}) (interface{}, error)
type StrictMiddlewareFunc func(f StrictHandlerFunc, operationID string) StrictHandlerFunc
func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
return &strictHandler{ssi: ssi, middlewares: middlewares}
func NewStrictHandler(ssi StrictServerInterface) ServerInterface {
return &strictHandler{ssi: ssi}
}
type strictHandler struct {
ssi StrictServerInterface
middlewares []StrictMiddlewareFunc
}
{{range .}}
{{$opid := .OperationId}}
// {{$opid}} operation middleware
// {{$opid}} operation
func (sh *strictHandler) {{.OperationId}}(ctx echo.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params {{.OperationId}}Params{{end}}) error {
var request {{$opid | ucFirst}}RequestObject
@@ -217,9 +214,6 @@ output-options:
handler := func(ctx echo.Context, request interface{}) (interface{}, error){
return sh.ssi.{{.OperationId}}(ctx, request.({{$opid | ucFirst}}RequestObject))
}
for _, middleware := range sh.middlewares {
handler = middleware(handler, "{{.OperationId}}")
}
response, err := handler(ctx, request)

File diff suppressed because it is too large Load Diff

View File

@@ -95,7 +95,7 @@ func newAPIService(config *server.ServerConfig) *apiService {
type APIServer struct {
config *server.ServerConfig
additionalMiddlewares []gen.StrictMiddlewareFunc
additionalMiddlewares []hatchetmiddleware.MiddlewareFunc
}
func NewAPIServer(config *server.ServerConfig) *APIServer {
@@ -125,7 +125,7 @@ func (t *APIServer) Run(opts ...APIServerExtensionOpt) (func() error, error) {
return nil, err
}
populator, err := t.registerSpec(g, spec)
populator, err := t.registerSpec(g, spec, t.additionalMiddlewares)
if err != nil {
return nil, err
@@ -139,7 +139,7 @@ func (t *APIServer) Run(opts ...APIServerExtensionOpt) (func() error, error) {
return t.RunWithServer(e)
}
func (t *APIServer) RunWithMiddlewares(middlewares []gen.StrictMiddlewareFunc, opts ...APIServerExtensionOpt) (func() error, error) {
func (t *APIServer) RunWithMiddlewares(middlewares []hatchetmiddleware.MiddlewareFunc, opts ...APIServerExtensionOpt) (func() error, error) {
t.additionalMiddlewares = middlewares
return t.Run(opts...)
@@ -178,20 +178,20 @@ func (t *APIServer) getCoreEchoService() (*echo.Echo, error) {
g := e.Group("")
if _, err := t.registerSpec(g, oaspec); err != nil {
if _, err := t.registerSpec(g, oaspec, t.additionalMiddlewares); err != nil {
return nil, err
}
service := newAPIService(t.config)
myStrictApiHandler := gen.NewStrictHandler(service, t.additionalMiddlewares)
myStrictApiHandler := gen.NewStrictHandler(service)
gen.RegisterHandlers(g, myStrictApiHandler)
return e, nil
}
func (t *APIServer) registerSpec(g *echo.Group, spec *openapi3.T) (*populator.Populator, error) {
func (t *APIServer) registerSpec(g *echo.Group, spec *openapi3.T, middlewares []hatchetmiddleware.MiddlewareFunc) (*populator.Populator, error) {
// application middleware
populatorMW := populator.NewPopulator(t.config)
@@ -429,6 +429,9 @@ func (t *APIServer) registerSpec(g *echo.Group, spec *openapi3.T) (*populator.Po
mw.Use(populatorMW.Middleware)
mw.Use(authnMW.Middleware)
mw.Use(authzMW.Middleware)
for _, m := range t.additionalMiddlewares {
mw.Use(m)
}
allHatchetMiddleware, err := mw.Middleware()