Skip to content

Commit d54c640

Browse files
committed
Added Cors responses
1 parent 9ebd380 commit d54c640

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pkg/httpresponse/httpresponse.go

+19
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,22 @@ func ErrorWith(w http.ResponseWriter, code int, v any, reason ...string) error {
144144
}
145145
return JSON(w, err, code, indent)
146146
}
147+
148+
// Cors is a utility function to set the CORS headers
149+
// on a pre-flight request. Setting origin to an empty
150+
// string or not including methods will allow any
151+
// origin and any method
152+
func Cors(w http.ResponseWriter, origin string, methods ...string) error {
153+
methods_ := "*"
154+
if len(methods) > 0 {
155+
methods_ = strings.ToUpper(strings.Join(methods, ","))
156+
}
157+
if origin == "" {
158+
origin = "*"
159+
}
160+
return Empty(w, http.StatusOK,
161+
"Access-Control-Allow-Origin", origin,
162+
"Access-Control-Allow-Methods", methods_,
163+
"Access-Control-Allow-Headers", "*",
164+
)
165+
}

0 commit comments

Comments
 (0)