mirror of
https://github.com/lus/pasty.git
synced 2023-08-10 21:13:09 +03:00
implicitly write 200 HTTP status code
This commit is contained in:
parent
c414fd7c59
commit
18839a2021
@ -7,6 +7,7 @@ import (
|
||||
"github.com/lus/pasty/internal/pastes"
|
||||
"github.com/lus/pasty/internal/reports"
|
||||
"github.com/lus/pasty/internal/storage"
|
||||
"github.com/lus/pasty/pkg/chiimplicitok"
|
||||
"github.com/lus/pasty/pkg/chizerolog"
|
||||
"net/http"
|
||||
)
|
||||
@ -52,6 +53,7 @@ func (server *Server) Start() error {
|
||||
|
||||
router.Use(chizerolog.Logger)
|
||||
router.Use(chizerolog.Recover)
|
||||
router.Use(chiimplicitok.Middleware)
|
||||
|
||||
// Register the web frontend handler
|
||||
router.Get("/*", frontendHandler(router.NotFoundHandler()))
|
||||
|
@ -15,5 +15,4 @@ func (server *Server) v2EndpointDeletePaste(writer http.ResponseWriter, request
|
||||
if err := server.Storage.Pastes().DeleteByID(request.Context(), paste.ID); err != nil {
|
||||
writeErr(request, writer, err)
|
||||
}
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
22
pkg/chiimplicitok/middleware.go
Normal file
22
pkg/chiimplicitok/middleware.go
Normal file
@ -0,0 +1,22 @@
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user