2023-06-17 18:05:34 +03:00
|
|
|
package chiimplicitok
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Middleware sets the status code of a request to http.StatusOK if it was not set explicitly by any handler.
|
|
|
|
func Middleware(next http.Handler) http.Handler {
|
|
|
|
fn := func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
proxy := middleware.NewWrapResponseWriter(writer, request.ProtoMajor)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if proxy.Status() == 0 {
|
|
|
|
proxy.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-17 19:20:25 +03:00
|
|
|
next.ServeHTTP(proxy, request)
|
2023-06-17 18:05:34 +03:00
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|