1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00
pasty/pkg/chiimplicitok/middleware.go

23 lines
546 B
Go
Raw Normal View History

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)
}
}()
next.ServeHTTP(writer, request)
}
return http.HandlerFunc(fn)
}