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

36 lines
848 B
Go
Raw Normal View History

2023-06-07 20:51:54 +03:00
package web
import (
"context"
"github.com/go-chi/chi/v5"
"net/http"
"strings"
)
func (server *Server) v2MiddlewareInjectPaste(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
pasteID := strings.TrimSpace(chi.URLParam(request, "paste_id"))
if pasteID == "" {
writeString(writer, http.StatusNotFound, "paste not found")
return
}
paste, err := server.Storage.Pastes().FindByID(request.Context(), pasteID)
if err != nil {
2023-06-17 18:00:44 +03:00
writeErr(request, writer, err)
2023-06-07 20:51:54 +03:00
}
if paste == nil {
writeString(writer, http.StatusNotFound, "paste not found")
return
}
if paste.Metadata == nil {
paste.Metadata = make(map[string]any)
}
request = request.WithContext(context.WithValue(request.Context(), "paste", paste))
next.ServeHTTP(writer, request)
})
}