Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CORS handler #144

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.namespaced-example.edn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
:triangulum.handler/private-response-keys #{}
:triangulum.handler/upload-max-size-mb 100
:triangulum.handler/upload-max-file-count 10
:triangulum.handler/cors-headers {"Access-Control-Allow-Origin" "https://example.com"
"Access-Control-Allow-Methods" "GET, POST, PUT, DELETE"
"Access-Control-Allow-Headers" "Content-Type, Authorization"
"Access-Control-Allow-Credentials" "true"}

;; workers (server)
:triangulum.worker/workers [{:triangulum.worker/name "scheduler"
Expand Down
6 changes: 6 additions & 0 deletions config.nested-example.edn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
:workers {:scheduler {:start product-ns.jobs/start-scheduled-jobs!
:stop product-ns.jobs/stop-scheduled-jobs!}}

;; cors
:cors-headers {"Access-Control-Allow-Origin" "https://example.com"
"Access-Control-Allow-Methods" "GET, POST, PUT, DELETE"
"Access-Control-Allow-Headers" "Content-Type, Authorization"
"Access-Control-Allow-Credentials" "true"}

;; response
:response-type :json} ; :edn or :transit

Expand Down
1 change: 1 addition & 0 deletions src/triangulum/config_namespaced_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
:triangulum.handler/bad-tokens
:triangulum.handler/upload-max-size-mb
:triangulum.handler/upload-max-file-count
:triangulum.handler/cors-headers
:triangulum.worker/workers
:triangulum.response/response-type])))

Expand Down
1 change: 1 addition & 0 deletions src/triangulum/config_nested_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:triangulum.handler/private-response-keys
:triangulum.handler/upload-max-size-mb
:triangulum.handler/upload-max-file-count
:triangulum.handler/cors-headers
:triangulum.worker/workers
:triangulum.response/response-type]))

Expand Down
16 changes: 16 additions & 0 deletions src/triangulum/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@
(cookie-store {:key (-> (random-string 16)
(string-to-bytes))}))

(defn wrap-cors-routes
"Adds CORS headers for specific routes"
[handler]
(fn [{:keys [uri headers] :as request}]
(let [cors-config (get-config :handler :cors-headers)
routes (->> (get-config :triangulum.handler/routing-tables)
(map (comp deref resolve-foreign-symbol))
(apply merge))
route (some (fn [[key value]]
(when (= (second key) uri)
{key value})) routes)]
(if (get-in route [1 :cors])
(handler (assoc request :headers (merge headers cors-config)))
(handler request)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Upload Configuration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -248,6 +263,7 @@
(wrap-content-type-options :nosniff)
wrap-response-logging
wrap-gzip
wrap-cors-routes
wrap-exceptions
(optional-middleware wrap-reload reload?)))

Expand Down
Loading