Created
June 17, 2020 15:11
-
-
Save cpjolicoeur/3352a87ffc0e1dd5a5347b3303fa8b4f to your computer and use it in GitHub Desktop.
CORS middleware for Joy framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn cors [handler &opt opts] | |
(default opts @{}) | |
(def defaults @{"Access-Control-Allow-Origin" "*" | |
"Access-Control-Allow-Methods" "GET, OPTIONS" | |
"Access-Control-Allow-Headers" "Content-Type" | |
"Access-Control-Max-Age 86400}) | |
(def options (merge defaults opts)) | |
(fn [request] | |
(if (= "OPTIONS" (get request :method)) | |
@{:status 204, :body "" :headers options} | |
(let [response (handler request)] | |
(when response | |
(update response :headers merge options)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment