Add public endpoint to expose images

This commit is contained in:
Deluan 2022-12-30 22:34:00 -05:00 committed by Deluan Quintão
parent 7fbcb2904a
commit 387acc5f63
9 changed files with 177 additions and 36 deletions

View file

@ -3,8 +3,6 @@ package nativeapi
import (
"context"
"net/http"
"net/url"
"strings"
"github.com/deluan/rest"
"github.com/go-chi/chi/v5"
@ -77,7 +75,7 @@ func (n *Router) RX(r chi.Router, pathPrefix string, constructor rest.Repository
r.Post("/", rest.Post(constructor))
}
r.Route("/{id}", func(r chi.Router) {
r.Use(urlParams)
r.Use(server.URLParamsMiddleware)
r.Get("/", rest.Get(constructor))
if persistable {
r.Put("/", rest.Put(constructor))
@ -92,7 +90,7 @@ func (n *Router) addPlaylistTrackRoute(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
getPlaylist(n.ds)(w, r)
})
r.With(urlParams).Route("/", func(r chi.Router) {
r.With(server.URLParamsMiddleware).Route("/", func(r chi.Router) {
r.Delete("/", func(w http.ResponseWriter, r *http.Request) {
deleteFromPlaylist(n.ds)(w, r)
})
@ -101,7 +99,7 @@ func (n *Router) addPlaylistTrackRoute(r chi.Router) {
})
})
r.Route("/{id}", func(r chi.Router) {
r.Use(urlParams)
r.Use(server.URLParamsMiddleware)
r.Put("/", func(w http.ResponseWriter, r *http.Request) {
reorderItem(n.ds)(w, r)
})
@ -111,26 +109,3 @@ func (n *Router) addPlaylistTrackRoute(r chi.Router) {
})
})
}
// Middleware to convert Chi URL params (from Context) to query params, as expected by our REST package
func urlParams(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := chi.RouteContext(r.Context())
parts := make([]string, 0)
for i, key := range ctx.URLParams.Keys {
value := ctx.URLParams.Values[i]
if key == "*" {
continue
}
parts = append(parts, url.QueryEscape(":"+key)+"="+url.QueryEscape(value))
}
q := strings.Join(parts, "&")
if r.URL.RawQuery == "" {
r.URL.RawQuery = q
} else {
r.URL.RawQuery += "&" + q
}
next.ServeHTTP(w, r)
})
}