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
+25
View File
@@ -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)
})
}