Merge pull request #802 from moonstream-to/fix-cors-nb

Fixed preflight requests for nb
pull/803/head
Sergei Sumarokov 2023-06-06 01:26:03 -07:00 zatwierdzone przez GitHub
commit 43a8e09d35
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 11 dodań i 9 usunięć

Wyświetl plik

@ -359,16 +359,18 @@ func panicMiddleware(next http.Handler) http.Handler {
// CORS middleware
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, allowedOrigin := range strings.Split(MOONSTREAM_CORS_ALLOWED_ORIGINS, ",") {
if r.Header.Get("Origin") == allowedOrigin {
w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
if r.Method == http.MethodOptions {
for _, allowedOrigin := range strings.Split(MOONSTREAM_CORS_ALLOWED_ORIGINS, ",") {
if r.Header.Get("Origin") == allowedOrigin {
w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
w.Header().Set("Access-Control-Allow-Methods", "GET,POST")
// Credentials are cookies, authorization headers, or TLS client certificates
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
}
}
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
// Credentials are cookies, authorization headers, or TLS client certificates
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Authorization")
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})