Add public endpoint to expose images
This commit is contained in:
@@ -5,9 +5,11 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
@@ -199,3 +201,26 @@ func firstOr(or string, strings ...string) string {
|
||||
}
|
||||
return or
|
||||
}
|
||||
|
||||
// URLParamsMiddleware convert Chi URL params (from Context) to query params, as expected by our REST package
|
||||
func URLParamsMiddleware(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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user