Skip to content

Commit

Permalink
exhttp: make auto-allow cors behavior optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 7, 2024
1 parent 5b69581 commit 07bba6a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions exhttp/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("{}"))
Expand Down

0 comments on commit 07bba6a

Please sign in to comment.