From 07bba6a0c5ebed98f7408d96d4af00bd265bdd3e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 8 Dec 2024 00:15:39 +0200 Subject: [PATCH] exhttp: make auto-allow cors behavior optional --- exhttp/json.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/exhttp/json.go b/exhttp/json.go index 48c8349..2717b0f 100644 --- a/exhttp/json.go +++ b/exhttp/json.go @@ -5,22 +5,30 @@ import ( "net/http" ) +var AutoAllowCORS = true + func WriteJSONResponse(w http.ResponseWriter, httpStatusCode int, jsonData any) { - AddCORSHeaders(w) + if AutoAllowCORS { + AddCORSHeaders(w) + } w.Header().Set("Content-Type", "application/json") w.WriteHeader(httpStatusCode) _ = json.NewEncoder(w).Encode(jsonData) } func WriteJSONData(w http.ResponseWriter, httpStatusCode int, data []byte) { - AddCORSHeaders(w) + if AutoAllowCORS { + AddCORSHeaders(w) + } w.Header().Set("Content-Type", "application/json") w.WriteHeader(httpStatusCode) _, _ = w.Write(data) } func WriteEmptyJSONResponse(w http.ResponseWriter, httpStatusCode int) { - AddCORSHeaders(w) + if AutoAllowCORS { + AddCORSHeaders(w) + } w.Header().Set("Content-Type", "application/json") w.WriteHeader(httpStatusCode) _, _ = w.Write([]byte("{}"))