Skip to content

Instantly share code, notes, and snippets.

@jaimesangcap
Forked from leordev/cors.clj
Created May 21, 2020 18:15
Show Gist options
  • Save jaimesangcap/b570e462b46570e6fa15d583c0462167 to your computer and use it in GitHub Desktop.
Save jaimesangcap/b570e462b46570e6fa15d583c0462167 to your computer and use it in GitHub Desktop.
(ns api-project.cors)
(def cors-headers
"Generic CORS headers"
{"Access-Control-Allow-Origin" "*"
"Access-Control-Allow-Headers" "*"
"Access-Control-Allow-Methods" "GET"})
(defn preflight?
"Returns true if the request is a preflight request"
[request]
(= (request :request-method) :options))
(defn all-cors
"Allow requests from all origins - also check preflight"
[handler]
(fn [request]
(if (preflight? request)
{:status 200
:headers cors-headers
:body "preflight complete"}
(let [response (handler request)]
(update-in response [:headers]
merge cors-headers )))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment