This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-07-13
Channels
- # babashka (5)
- # beginners (55)
- # calva (24)
- # cider (11)
- # cljsrn (14)
- # clojure (55)
- # clojure-europe (9)
- # clojure-houston (1)
- # clojure-italy (1)
- # clojure-nl (7)
- # clojure-uk (38)
- # clojurescript (4)
- # code-reviews (12)
- # cursive (3)
- # datomic (86)
- # figwheel-main (14)
- # helix (10)
- # hoplon (4)
- # interceptors (1)
- # jobs (2)
- # jobs-discuss (13)
- # kaocha (5)
- # leiningen (5)
- # local-first-clojure (1)
- # luminus (2)
- # malli (27)
- # membrane (1)
- # off-topic (43)
- # pedestal (5)
- # re-frame (32)
- # reagent (22)
- # reitit (24)
- # remote-jobs (2)
- # shadow-cljs (1)
- # spacemacs (1)
- # sql (1)
- # tools-deps (32)
- # xtdb (51)
I wrote a quick wrap-cors
middleware:
(defn wrap-cors [handler]
(fn [request]
(let [res (handler request)]
(if-not res
res
(-> res
(assoc-in [:headers "Access-Control-Allow-Origin"] "*")
(assoc-in [:headers "Access-Control-Allow-Headers"] "*"))))))
am a bit unhappy with the if-not
-- if you think there's a more concise way I can express this would love thoughts : )(defn wrap-cors [handler]
(fn [request]
(some-> (handler request)
(assoc-in [:headers "Access-Control-Allow-Origin"] "*")
(assoc-in [:headers "Access-Control-Allow-Headers"] "*"))))
also, the double assoc in could just be an update-in (update-in [:headers] assoc "Access-Control-Allow-Origin" "**" "Access-Control-Allow-Headers" "**")
another way to replace (if-not x x ...)
is (and x ...)
it has the same conditional branching and delayed evaluation as if
FYI @stopachka There's an existing middleware library for that https://github.com/r0man/ring-cors which handles the preflight checking (which your code does not).
👍 6