add cors handle
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
e22e8d4e5a
commit
5a5312097d
2 changed files with 47 additions and 7 deletions
|
|
@ -14,7 +14,8 @@ type Config struct {
|
||||||
CoinGeckoKey string
|
CoinGeckoKey string
|
||||||
PriceFetchInterval time.Duration
|
PriceFetchInterval time.Duration
|
||||||
PriceCleanInterval time.Duration
|
PriceCleanInterval time.Duration
|
||||||
SnapshotHorizonDays int // nombre de jours dans le futur couverts par les snapshots
|
SnapshotHorizonDays int
|
||||||
|
CORSAllowedOrigins string // "*" ou liste comma-séparée d'origines
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load() *Config {
|
func Load() *Config {
|
||||||
|
|
@ -37,6 +38,7 @@ func Load() *Config {
|
||||||
PriceFetchInterval: fetchInterval,
|
PriceFetchInterval: fetchInterval,
|
||||||
PriceCleanInterval: 24 * time.Hour,
|
PriceCleanInterval: 24 * time.Hour,
|
||||||
SnapshotHorizonDays: horizonDays,
|
SnapshotHorizonDays: horizonDays,
|
||||||
|
CORSAllowedOrigins: getenv("CORS_ALLOWED_ORIGINS", "*"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.g3e.fr/H6N/account/internal/auth"
|
"git.g3e.fr/H6N/account/internal/auth"
|
||||||
"git.g3e.fr/H6N/account/internal/config"
|
"git.g3e.fr/H6N/account/internal/config"
|
||||||
|
|
@ -33,12 +34,49 @@ func New(cfg *config.Config, pool *pgxpool.Pool, logger *slog.Logger) *Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// /health est exempt d'auth. Toutes les autres routes passent par le middleware owner.
|
s.corsMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path == "/health" {
|
if r.URL.Path == "/health" {
|
||||||
s.mux.ServeHTTP(w, r)
|
s.mux.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
auth.Middleware(s.mux).ServeHTTP(w, r)
|
auth.Middleware(s.mux).ServeHTTP(w, r)
|
||||||
|
})).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) corsMiddleware(next http.Handler) http.Handler {
|
||||||
|
allowedOrigins := strings.Split(s.cfg.CORSAllowedOrigins, ",")
|
||||||
|
wildcard := len(allowedOrigins) == 1 && strings.TrimSpace(allowedOrigins[0]) == "*"
|
||||||
|
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
origin := r.Header.Get("Origin")
|
||||||
|
if origin != "" {
|
||||||
|
allowed := wildcard
|
||||||
|
if !allowed {
|
||||||
|
for _, o := range allowedOrigins {
|
||||||
|
if strings.TrimSpace(o) == origin {
|
||||||
|
allowed = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if allowed {
|
||||||
|
if wildcard {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
w.Header().Add("Vary", "Origin")
|
||||||
|
}
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, X-Owner-ID")
|
||||||
|
w.Header().Set("Access-Control-Max-Age", "86400")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) routes() {
|
func (s *Server) routes() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue