using ring-cors/wrap-cors

2,018 views
Skip to first unread message

Christian Jauvin

unread,
Sep 30, 2013, 12:00:08 AM9/30/13
to lumin...@googlegroups.com
Hi,

I'm new to Clojure, and even newer to Luminus, and my learning project is to slowly port a Python webapp I wrote.

Since I want to do this porting slowly, component by component, I need to run both the Python and Luminus servers side by side (each on a different port). I'm pretty sure this is doable with CORS, and so I assume ring-cors might help, but the problem is that I'm really not sure how to do this. I tried adding wrap-cors in the :middleware list of the app-handler:

(def app (middleware/app-handler
           [home-routes app-routes]
           :middleware [wrap-cors]
           :access-rules []
           :formats [:json-kw :edn]))

but it doesn't seem to be sufficient. I guess I need to add a :access-control-allow-origin param like the ring-cors doc mentions:


but I can't find any example close enough in the doc or source code to let me infer how to do this. So am I on the right track, is what I want to do doable, and if so, how do I do it?

Many thanks,

Christian


Dmitri

unread,
Sep 30, 2013, 12:57:52 AM9/30/13
to lumin...@googlegroups.com
I haven't used CORS, but from looking at the docs it seems like you have to provide wrap-cors with the :access-control-allow-origin key. The following might work better

:middleware [#(wrap-cors % :access-control-allow-origin #"http://yoursite.com")]

Christian Jauvin

unread,
Sep 30, 2013, 10:16:50 AM9/30/13
to Dmitri, lumin...@googlegroups.com
Thanks Dmitri, I didn't think of using an anonymous function, but that was the solution indeed. For the record, in my case I had to specify the :access-control-allow-headers as well to make it work:

:middleware [#(wrap-cors % :access-control-allow-origin #"http://localhost:81"
                           :access-control-allow-headers ["Origin" "X-Requested-With"
                                                          "Content-Type" "Accept"])]

where by the way 81 is the port of my Python Flask server (while Luminus runs on 3000).

I would suggest adding such an example (i.e. requiring an anonymous function) in the "Adding custom middleware" section of the Luminus doc, as it might be helpful to some. If you accept pull requests for the doc, I could try to do it.


Dmitri

unread,
Sep 30, 2013, 10:19:13 AM9/30/13
to lumin...@googlegroups.com, Dmitri
Glad that worked and I'll add an example as you're right that it's not obvious you have to do that.
Reply all
Reply to author
Forward
0 new messages