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:
parent
e8a88e21ae
commit
9a794a82d0
@ -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
|
|
||||||
}
|
|
@ -3,6 +3,7 @@ package web
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/lus/pasty/internal/meta"
|
"github.com/lus/pasty/internal/meta"
|
||||||
"github.com/lus/pasty/internal/pastes"
|
"github.com/lus/pasty/internal/pastes"
|
||||||
"github.com/lus/pasty/internal/reports"
|
"github.com/lus/pasty/internal/reports"
|
||||||
@ -67,11 +68,11 @@ func (server *Server) Start() error {
|
|||||||
// Register the paste API endpoints
|
// Register the paste API endpoints
|
||||||
router.Get("/api/*", router.NotFoundHandler())
|
router.Get("/api/*", router.NotFoundHandler())
|
||||||
router.With(server.v2MiddlewareInjectPaste).Get("/api/v2/pastes/{paste_id}", server.v2EndpointGetPaste)
|
router.With(server.v2MiddlewareInjectPaste).Get("/api/v2/pastes/{paste_id}", server.v2EndpointGetPaste)
|
||||||
router.Post("/api/v2/pastes", server.v2EndpointCreatePaste)
|
router.With(middleware.AllowContentType("application/json")).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"), 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)
|
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
|
||||||
if server.ReportClient != nil {
|
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) {
|
router.Get("/api/v2/info", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
writeJSONOrErr(request, writer, http.StatusOK, map[string]any{
|
writeJSONOrErr(request, writer, http.StatusOK, map[string]any{
|
||||||
|
@ -16,9 +16,6 @@ type v2EndpointCreatePastePayload struct {
|
|||||||
|
|
||||||
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
|
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
|
||||||
// Read, parse and validate the request payload
|
// Read, parse and validate the request payload
|
||||||
if !accept(writer, request, "application/json") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(request.Body)
|
body, err := io.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(request, writer, err)
|
writeErr(request, writer, err)
|
||||||
|
@ -20,9 +20,6 @@ func (server *Server) v2EndpointModifyPaste(writer http.ResponseWriter, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read, parse and validate the request payload
|
// Read, parse and validate the request payload
|
||||||
if !accept(writer, request, "application/json") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(request.Body)
|
body, err := io.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(request, writer, err)
|
writeErr(request, writer, err)
|
||||||
|
@ -20,9 +20,6 @@ func (server *Server) v2EndpointReportPaste(writer http.ResponseWriter, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read, parse and validate the request payload
|
// Read, parse and validate the request payload
|
||||||
if !accept(writer, request, "application/json") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(request.Body)
|
body, err := io.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(request, writer, err)
|
writeErr(request, writer, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user