reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
grounded_sage 2021-04-23T10:32:41.148Z

Is there a simple API first reitit demo. Curious as I am having trouble getting past cors issue. Not sure what I am missing.

grounded_sage 2021-04-23T10:35:22.148400Z

What am I missing here?

(defn handler [_]
  {:status 200, :body "hello app"})

(def app
  (ring/ring-handler
   (ring/router
    ["/" {:get handler}]
    {:exception pretty/exception
     :data {:middleware [[wrap-cors :access-control-allow-origin [#".*"]
                          :access-control-allow-methods [:get :put :post :patch :delete]]]}})
   (ring/create-default-handler {:not-found (constantly {:status 404 :body "Not found"})})))

emil0r 2021-04-23T10:59:10.148500Z

If you're using cljs-ajax you need withCredentials and corresponding middleware in the backend

emil0r 2021-04-23T11:00:07.148600Z

And you need :options for CORS iirc for the preflight check from the browser

grounded_sage 2021-04-23T11:49:53.150Z

using cljs-http on the frontend. This answer still confuses me. Would be easier if I could see a code example. All ones I have seen in the wild do not work for me.

emil0r 2021-04-23T14:32:16.150200Z

I can put together something later if no one else beats me to it

๐Ÿ‘ 1
emil0r 2021-04-23T14:33:11.150900Z

See if you can move forward by adding :options to the CORS middleware

grounded_sage 2021-04-23T14:35:54.153700Z

I find it strange how I've found like 5 different examples on how to handle CORS. Hard to tell which one is the more idiomatic way to do it in reitit. Given this is like a fundamental piece I would think there would be documentation right there on the library. Even if it is until a more bundled approach is achieved.

ikitommi 2021-04-23T14:51:31.155500Z

see https://github.com/metosin/reitit/issues/236

ikitommi 2021-04-23T14:52:03.155800Z

PR welcome.

ikitommi 2021-04-23T14:53:07.156Z

would love to see cors, security headers etc. as builtins. 100% busy atm, so, contributions welcome.

miikka 2021-04-23T14:57:18.156800Z

Just released reitit 0.5.13. A minor fix to create-resource-handler/create-file-handler and some doc and deps updates. https://github.com/metosin/reitit/blob/master/CHANGELOG.md#0513-2021-04-23

๐ŸŽ‰ 3
grounded_sage 2021-04-23T15:00:13.157500Z

I've tried: The interceptor way with this library: https://github.com/zerg000000/simple-cors The {:data ...} pattern found here: https://github.com/prestancedesign/todo-backend-clojure-reitit/blob/master/src/todo_backend/core.clj This example here: https://github.com/metosin/reitit/issues/143#issuecomment-421781636 The example here (which uses options). https://clojurians-log.clojureverse.org/reitit/2020-05-10

miikka 2021-04-23T15:01:03.157600Z

What's the CORS issue you're having?

grounded_sage 2021-04-23T15:03:04.157800Z

Access to XMLHttpRequest at '<http://localhost:9090/>' from origin '<http://localhost:3000>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

miikka 2021-04-23T15:04:56.158Z

Ok, let me quickly try this out

miikka 2021-04-23T15:12:45.158200Z

Hmh, I wonder what is going on. I tried the code with wrap-cors you pasted above and the following JavaScript served from a separate port and it seemed to work just fine:

fetch('<http://localhost:3000/').then(function(resp)> {
    return resp.text();
}).then(function(body) {
    console.log("body text:", body);
});

miikka 2021-04-23T15:13:15.158400Z

Looking at the browser developer tools, the response has the CORS headers it should have (`Access-Control-Allow-Origin: http://localhost:8000` etc)

grounded_sage 2021-04-23T15:19:05.158800Z

weird

grounded_sage 2021-04-23T15:19:13.159Z

No idea why I can't get it working then

miikka 2021-04-23T15:23:50.159300Z

https://github.com/miikka/reitit-cors-test here's the code i used. I mean, it just the code you posted, but still.

miikka 2021-04-23T15:26:02.159600Z

Hmm, right, is it a GET request or something else that you're doing?

grounded_sage 2021-04-23T15:27:08.159800Z

Yes was a GET request

miikka 2021-04-23T15:31:19.160Z

Ok. That should be the simplest case

miikka 2021-04-23T15:31:32.160200Z

Does the response from the server look otherwise okay? Like the status code, response body, tec?

grounded_sage 2021-04-23T15:31:55.160400Z

{:status 0, :success false, :body "", :headers {}, :trace-redirects ["<http://localhost:9000>" "<http://localhost:9000>"], :error-code :http-error, :error-text " [0]"}

miikka 2021-04-23T15:32:28.160600Z

Right, I meant if you look at it in the browser debugger

miikka 2021-04-23T15:32:51.160800Z

Network tab in Firefox developer tools

miikka 2021-04-23T15:33:05.161Z

Well, it's called Network in Chrome as well

grounded_sage 2021-04-23T15:33:28.161200Z

Has a CORS error

grounded_sage 2021-04-23T15:34:28.161400Z

miikka 2021-04-23T15:37:52.161900Z

Right, yeah

miikka 2021-04-23T15:38:12.162100Z

The debugger does not actually show that stuff when there's CORS error. Ah well.

grounded_sage 2021-04-23T15:43:58.162300Z

Could it possibly be from the frontend doing something wrong?

grounded_sage 2021-04-23T15:45:26.162500Z

This is basically what I have atm. Obviously port being what the port is for server.

(ns app.core
  (:require [cljs-http.client :as http]
            [cljs.core.async :refer [go &lt;!]]))

(go (let [response (&lt;! (http/get "<http://localhost:9000>"))]
      (prn response)
      (prn (:status response))
      (prn (:body response))))

miikka 2021-04-23T15:45:41.162700Z

Not sure, I think cljs-http should just work with the default settings?

miikka 2021-04-23T15:45:49.162900Z

I'm starting to run out of ideas. You could try to run the same request with curl and check that the output looks right, but otherwise I don't know what to do. (You can right-click that request in Chrome Network tab and select Copy -> Copy as cURL to get the ready-to-run command-line command)

grounded_sage 2021-04-23T15:47:02.163100Z

haha the copy as curl works fine :man-shrugging::skin-tone-2:

miikka 2021-04-23T15:47:50.163300Z

Hmm, I wonder if (http/get "<http://localhost:9000/>"{:with-credentials? false}) would help

๐Ÿ‘ 1
grounded_sage 2021-04-23T15:50:16.163500Z

Iย think there is something wrong with this client side http library

miikka 2021-04-23T15:52:00.163700Z

Could be. I've successfully used it in CORS context previously though

miikka 2021-04-23T15:53:20.163900Z

Ah well. I have to go now. I hope you'll figure it out!

grounded_sage 2021-04-23T15:55:17.164200Z

That was it. Thanks! Super annoying. Thank you for helping me spot that!