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

23 lines
545 B
Go

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