From 2b4569085881207de5f1c39d8240f70b3218fd59 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 1 Jul 2019 19:40:35 +0200 Subject: [PATCH] feat(backend): improve cors middleware and config --- backend/src/uxbox/config.clj | 1 + backend/src/uxbox/http/middleware.clj | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/src/uxbox/config.clj b/backend/src/uxbox/config.clj index 7bbcf1837..68c834f1e 100644 --- a/backend/src/uxbox/config.clj +++ b/backend/src/uxbox/config.clj @@ -35,6 +35,7 @@ [] {:http-server-port (lookup-env env :uxbox-http-server-port 6060) :http-server-debug (lookup-env env :uxbox-http-server-debug true) + :http-server-cors (lookup-env env :uxbox-http-server-cors "http://localhost:3449") :database-username (lookup-env env :uxbox-database-username nil) :database-password (lookup-env env :uxbox-database-password nil) :database-name (lookup-env env :uxbox-database-name "uxbox") diff --git a/backend/src/uxbox/http/middleware.clj b/backend/src/uxbox/http/middleware.clj index 043aa5f84..c08620eae 100644 --- a/backend/src/uxbox/http/middleware.clj +++ b/backend/src/uxbox/http/middleware.clj @@ -16,6 +16,7 @@ [ring.middleware.session :refer [wrap-session]] [ring.middleware.session.cookie :refer [cookie-store]] [ring.middleware.multipart-params :refer [wrap-multipart-params]] + [uxbox.config :as cfg] [uxbox.http.etag :refer [wrap-etag]] [uxbox.http.cors :refer [wrap-cors]] [uxbox.http.errors :as errors] @@ -112,7 +113,7 @@ :wrap #(wrap-session % options)})) (def cors-conf - {:origin #{"http://127.0.0.1:3449"} + {:origin #{"http://localhost:3449"} :max-age 3600 :allow-credentials true :allow-methods #{:post :put :get :delete} @@ -120,7 +121,12 @@ (def ^:private cors-middleware {:name ::cors-middleware - :wrap #(wrap-cors % cors-conf)}) + :wrap (fn [handler] + (let [cors (:http-server-cors cfg/config)] + (if (string? cors) + (->> (assoc cors-conf :origin #{cors}) + (wrap-cors handler)) + handler)))}) (def ^:private etag-middleware {:name ::etag-middleware