From 6cb884f86fbb45f9516367573f588bf258f0e599 Mon Sep 17 00:00:00 2001 From: John Eastman Date: Mon, 26 Sep 2016 09:10:53 -0500 Subject: [PATCH 1/2] Add CORS support for http endpoint. --- project.clj | 3 ++- src/zimbra/simioj/endpoint/http.clj | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/project.clj b/project.clj index 14450fd..f1018c2 100644 --- a/project.clj +++ b/project.clj @@ -19,7 +19,8 @@ [org.clojure/core.async "0.2.374"] ;; the following two entries are to support sqlite [org.clojure/java.jdbc "0.3.7"] - [org.xerial/sqlite-jdbc "3.8.9"]] + [org.xerial/sqlite-jdbc "3.8.9"] + [ring-cors "0.1.8"]] :main ^:skip-aot zimbra.simioj.main :target-path "target/%s" :profiles {:uberjar {:aot :all} diff --git a/src/zimbra/simioj/endpoint/http.clj b/src/zimbra/simioj/endpoint/http.clj index 7750da0..6feb955 100644 --- a/src/zimbra/simioj/endpoint/http.clj +++ b/src/zimbra/simioj/endpoint/http.clj @@ -6,6 +6,7 @@ [compojure.core :refer :all] [ring.adapter.jetty :refer [run-jetty]] [ring.util.request :refer :all] + [ring.middleware.cors :refer [wrap-cors]] [zimbra.simioj.endpoint.middleware :refer :all] [zimbra.simioj.raft.server :refer :all]) (:gen-class)) @@ -145,9 +146,12 @@ POST - Post a command to the Raft (defn- endpoint-app "Wraps the provided routes with json or edn filtering" [ctx] - (wrap-response + (wrap-cors + (wrap-response (routes - (app-routes ctx)))) + (app-routes ctx))) + :access-control-allow-origin [#".*"] + :access-control-allow-methods [:get :post])) (defn start! From 8b64b7be0a72ed8690e770534794205ff2dc68a0 Mon Sep 17 00:00:00 2001 From: John Eastman Date: Mon, 26 Sep 2016 14:13:35 -0500 Subject: [PATCH 2/2] Add CORS options to config. --- resources/defaults.edn | 6 +++++- src/zimbra/simioj/endpoint/http.clj | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/resources/defaults.edn b/resources/defaults.edn index f8078c5..9e894f2 100644 --- a/resources/defaults.edn +++ b/resources/defaults.edn @@ -4,7 +4,11 @@ :name "node0" ;; Inter-node communications :endpoint {;; HTTP address:port to bind to - :http "0.0.0.0:2283"} + :http "0.0.0.0:2283" + ;; CORS + :access-control-allow-origin [".*"] + :access-control-allow-methods [:get :post] + } ;; Jetty configuration :worker {:count 4 :threads 4} ;; This location is added to the classpath and should contain diff --git a/src/zimbra/simioj/endpoint/http.clj b/src/zimbra/simioj/endpoint/http.clj index 6feb955..d1118c4 100644 --- a/src/zimbra/simioj/endpoint/http.clj +++ b/src/zimbra/simioj/endpoint/http.clj @@ -150,8 +150,11 @@ POST - Post a command to the Raft (wrap-response (routes (app-routes ctx))) - :access-control-allow-origin [#".*"] - :access-control-allow-methods [:get :post])) + :access-control-allow-origin + (map re-pattern + (get-in @ctx [:config :node :endpoint :access-control-allow-origin] ".*")) + :access-control-allow-methods + (get-in @ctx [:config :node :endpoint :access-control-allow-methods] [:get :post]))) (defn start!