Skip to content

Commit 07bba6a

Browse files
committed
exhttp: make auto-allow cors behavior optional
1 parent 5b69581 commit 07bba6a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

exhttp/json.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@ import (
55
"net/http"
66
)
77

8+
var AutoAllowCORS = true
9+
810
func WriteJSONResponse(w http.ResponseWriter, httpStatusCode int, jsonData any) {
9-
AddCORSHeaders(w)
11+
if AutoAllowCORS {
12+
AddCORSHeaders(w)
13+
}
1014
w.Header().Set("Content-Type", "application/json")
1115
w.WriteHeader(httpStatusCode)
1216
_ = json.NewEncoder(w).Encode(jsonData)
1317
}
1418

1519
func WriteJSONData(w http.ResponseWriter, httpStatusCode int, data []byte) {
16-
AddCORSHeaders(w)
20+
if AutoAllowCORS {
21+
AddCORSHeaders(w)
22+
}
1723
w.Header().Set("Content-Type", "application/json")
1824
w.WriteHeader(httpStatusCode)
1925
_, _ = w.Write(data)
2026
}
2127

2228
func WriteEmptyJSONResponse(w http.ResponseWriter, httpStatusCode int) {
23-
AddCORSHeaders(w)
29+
if AutoAllowCORS {
30+
AddCORSHeaders(w)
31+
}
2432
w.Header().Set("Content-Type", "application/json")
2533
w.WriteHeader(httpStatusCode)
2634
_, _ = w.Write([]byte("{}"))

0 commit comments

Comments
 (0)