3

I am using ring adapter jetty server in my compojure api project. Now I want to add ring middleware CORS to my Project. How should I add and where should I add ring middleware CORS in my project?

These are my project code snippets

API

(ns clojure-dauble-business-api.core
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [clojure-dauble-business-api.logic :as logic]
            [clojure.tools.logging :as log]
            [clojure-dauble-business-api.domain.artwork]
            [cheshire.core :as json])
  (:import [clojure_dauble_business_api.domain.artwork Artwork]))


(defapi app
  (GET ["/hello/:id", :id #"[0-9]+"] [id :as request]
    (log/info "Function begins from here" request)
    (def jsonString (json/generate-string (get-in request [:headers])))
    (log/info "Create - header value is " (get-in (json/parse-string jsonString true) [:accesstoken]))
    (def artworkData (logic/artwork-id (->> id (re-find #"\d+") Long/parseLong)))
    (def data (if (not-empty artworkData)
               {:data artworkData :status 200}
               {:data [] :status 201}))
   (ok data)))

Method to Run the api on Jetty

(ns clojure-dauble-business-api.routes
    (:require [compojure.core :refer :all]
              [ring.adapter.jetty :as jetty]
              [ring.middleware.cors :refer [wrap-cors]]
             (clojure-dauble-business-api [core :as core]
                                          [test :as t])))
(def app
 (routes core/app t/test))


(jetty/run-jetty app {:port 3000})

Here core/app is the above API function path and t/test another API function(I have not provided the code here).

4

0 回答 0