1
0
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:
Lukas Schulte Pelkum 2023-06-17 17:05:34 +02:00
parent c414fd7c59
commit 18839a2021
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
3 changed files with 24 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/lus/pasty/internal/pastes" "github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/reports" "github.com/lus/pasty/internal/reports"
"github.com/lus/pasty/internal/storage" "github.com/lus/pasty/internal/storage"
"github.com/lus/pasty/pkg/chiimplicitok"
"github.com/lus/pasty/pkg/chizerolog" "github.com/lus/pasty/pkg/chizerolog"
"net/http" "net/http"
) )
@ -52,6 +53,7 @@ func (server *Server) Start() error {
router.Use(chizerolog.Logger) router.Use(chizerolog.Logger)
router.Use(chizerolog.Recover) router.Use(chizerolog.Recover)
router.Use(chiimplicitok.Middleware)
// Register the web frontend handler // Register the web frontend handler
router.Get("/*", frontendHandler(router.NotFoundHandler())) router.Get("/*", frontendHandler(router.NotFoundHandler()))

View File

@ -15,5 +15,4 @@ func (server *Server) v2EndpointDeletePaste(writer http.ResponseWriter, request
if err := server.Storage.Pastes().DeleteByID(request.Context(), paste.ID); err != nil { if err := server.Storage.Pastes().DeleteByID(request.Context(), paste.ID); err != nil {
writeErr(request, writer, err) writeErr(request, writer, err)
} }
writer.WriteHeader(http.StatusOK)
} }

View 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)
}