Skip to content

Commit

Permalink
Server: add wrap-cors> sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
zarlen committed May 20, 2015
1 parent 6bf9235 commit 686f0e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions full.http/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[http-kit "2.1.18"]
[compojure "1.3.1"]
[javax.servlet/servlet-api "2.5"]
[ring-cors "0.1.7"]
[fullcontact/camelsnake "0.4.15-SNAPSHOT"]
[fullcontact/full.json "0.4.15-SNAPSHOT"]
[fullcontact/full.metrics "0.4.15-SNAPSHOT"]
Expand Down
15 changes: 14 additions & 1 deletion full.http/src/full/http/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
[full.core.log :as log]
[full.async :refer :all]
[full.json :refer [write-json]]
[full.metrics :as metrics])
[full.metrics :as metrics]
[ring.middleware.cors :as rc])
(:import (clojure.core.async.impl.protocols ReadPort)
(clojure.lang ExceptionInfo)
(org.httpkit HttpStatus)))
Expand Down Expand Up @@ -48,6 +49,18 @@
:status (or (:status res) 200)
:headers (or (:headers res) {}))))))

(defn wrap-cors>
[handler & access-control]
(let [access-control (rc/normalize-config access-control)]
(fn [request]
(go-try
(cond
(and (rc/preflight? request) (rc/allow-request? request access-control))
{:status 200 :headers {}}
(rc/allow-request? request access-control)
(rc/add-access-control request access-control (<? (handler request)))
:else (<? (handler request)))))))

(def default-logger (org.slf4j.LoggerFactory/getLogger "full.http.server.request"))

(defn response-error-status [ex]
Expand Down

0 comments on commit 686f0e2

Please sign in to comment.