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

use native middleware.AllowContentType

This commit is contained in:
Lukas Schulte Pelkum 2023-06-17 20:07:19 +02:00
parent e8a88e21ae
commit 9a794a82d0
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
5 changed files with 4 additions and 26 deletions

View File

@ -1,14 +0,0 @@
package web
import "net/http"
func accept(writer http.ResponseWriter, request *http.Request, contentTypes ...string) bool {
contentType := request.Header.Get("Content-Type")
for _, accepted := range contentTypes {
if contentType == accepted {
return true
}
}
writeString(writer, http.StatusUnsupportedMediaType, "unsupported media type")
return false
}

View File

@ -3,6 +3,7 @@ package web
import (
"context"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/lus/pasty/internal/meta"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/reports"
@ -67,11 +68,11 @@ func (server *Server) Start() error {
// Register the paste API endpoints
router.Get("/api/*", router.NotFoundHandler())
router.With(server.v2MiddlewareInjectPaste).Get("/api/v2/pastes/{paste_id}", server.v2EndpointGetPaste)
router.Post("/api/v2/pastes", server.v2EndpointCreatePaste)
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Patch("/api/v2/pastes/{paste_id}", server.v2EndpointModifyPaste)
router.With(middleware.AllowContentType("application/json")).Post("/api/v2/pastes", server.v2EndpointCreatePaste)
router.With(middleware.AllowContentType("application/json"), server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Patch("/api/v2/pastes/{paste_id}", server.v2EndpointModifyPaste)
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
if server.ReportClient != nil {
router.With(server.v2MiddlewareInjectPaste).Post("/api/v2/pastes/{paste_id}/report", server.v2EndpointReportPaste)
router.With(middleware.AllowContentType("application/json"), server.v2MiddlewareInjectPaste).Post("/api/v2/pastes/{paste_id}/report", server.v2EndpointReportPaste)
}
router.Get("/api/v2/info", func(writer http.ResponseWriter, request *http.Request) {
writeJSONOrErr(request, writer, http.StatusOK, map[string]any{

View File

@ -16,9 +16,6 @@ type v2EndpointCreatePastePayload struct {
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
// Read, parse and validate the request payload
if !accept(writer, request, "application/json") {
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
writeErr(request, writer, err)

View File

@ -20,9 +20,6 @@ func (server *Server) v2EndpointModifyPaste(writer http.ResponseWriter, request
}
// Read, parse and validate the request payload
if !accept(writer, request, "application/json") {
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
writeErr(request, writer, err)

View File

@ -20,9 +20,6 @@ func (server *Server) v2EndpointReportPaste(writer http.ResponseWriter, request
}
// Read, parse and validate the request payload
if !accept(writer, request, "application/json") {
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
writeErr(request, writer, err)