nsfwapp/backend/cors.go
2026-02-06 10:28:46 +01:00

31 lines
953 B
Go

package main
import (
"net/http"
"strings"
)
func withCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := strings.TrimSpace(r.Header.Get("Origin"))
// Dev-Origins erlauben
if origin == "http://localhost:5173" || origin == "http://127.0.0.1:5173" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,DELETE,HEAD,OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Range, Last-Event-ID")
w.Header().Set("Access-Control-Expose-Headers", "Content-Length, Content-Range, Accept-Ranges")
// Nur wenn du wirklich Cookies/Authorization cross-origin brauchst:
// w.Header().Set("Access-Control-Allow-Credentials", "true")
}
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}