Skip to content

Commit

Permalink
[Gorilla] update the MiddlewareFunc type for compatibility
Browse files Browse the repository at this point in the history
This PR ported the fix deepmap#578
to gorilla/mux, so the gorilla/mux users can have a consistent behaviour
too.

Signed-off-by: Mengnan Gong <namco1992@gmail.com>
  • Loading branch information
namco1992 committed Aug 8, 2023
1 parent d26d251 commit 6b066d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 13 additions & 13 deletions examples/petstore-expanded/gorilla/api/petstore.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/codegen/templates/gorilla/gorilla-middleware.tmpl
Expand Up @@ -5,7 +5,7 @@ type ServerInterfaceWrapper struct {
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
}

type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc
type MiddlewareFunc func(http.Handler) http.Handler

{{range .}}{{$opid := .OperationId}}

Expand Down Expand Up @@ -173,9 +173,9 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
{{end}}
{{end}}

handler := func(w http.ResponseWriter, r *http.Request) {
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
siw.Handler.{{.OperationId}}(w, r{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}})
}
}))

{{if opts.Compatibility.ApplyGorillaMiddlewareFirstToLast}}
for i := len(siw.HandlerMiddlewares) -1; i >= 0; i-- {
Expand All @@ -187,7 +187,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
}
{{end}}

handler(w, r.WithContext(ctx))
handler.ServeHTTP(w, r.WithContext(ctx))
}
{{end}}

Expand Down

0 comments on commit 6b066d4

Please sign in to comment.