2023-10-16 22:16:20 +00:00
|
|
|
package server
|
2023-10-04 13:49:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-12-08 04:24:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/rs/cors"
|
2023-10-04 13:49:42 +00:00
|
|
|
)
|
|
|
|
|
2023-12-08 04:24:18 +00:00
|
|
|
// NormalizeQueryValuesHandler normalizes an input query of "key=value1,value2,value3" to "key=value1&key=value2&key=value3"
|
2023-10-16 22:16:20 +00:00
|
|
|
func NormalizeQueryValuesHandler(next http.Handler) http.Handler {
|
2023-10-04 13:49:42 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
2023-10-16 22:16:20 +00:00
|
|
|
NormalizeQueryValues(query)
|
2023-10-04 13:49:42 +00:00
|
|
|
r.URL.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2023-12-08 04:24:18 +00:00
|
|
|
|
|
|
|
// CorsHandler sets the cors settings on api endpoints
|
|
|
|
func CorsHandler(allowOrigins []string) mux.MiddlewareFunc {
|
|
|
|
c := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: allowOrigins,
|
|
|
|
AllowedMethods: []string{http.MethodPost, http.MethodGet, http.MethodDelete, http.MethodOptions},
|
|
|
|
AllowCredentials: true,
|
|
|
|
MaxAge: 600,
|
|
|
|
AllowedHeaders: []string{"*"},
|
|
|
|
})
|
|
|
|
|
|
|
|
return c.Handler
|
|
|
|
}
|