Fork me on GitHub
#reitit
<
2020-10-26
>
motform08:10:19

Maybe an obvious question, but how do I redirect SPA routing of all non /api urls for an app that is served using ring-reitit? On Netlify, this is achieved using a _redirects file (`/* /index.html 200`), but for this case I guess I need to create a route?

ikitommi17:10:14

@love.lagerkvist you should use either a default handler (https://cljdoc.org/d/metosin/reitit/0.5.10/doc/ring/default-handler) or add a catch-all route in the route tree, e.g. ["/*" {:get index-handler}]

ingesol19:10:12

So I got reitit+malli working nicely for browser routing, coercing path params into proper values. Now I want to coerce the other way, encoding my values into url strings again. (reitit-frontend/match-by-path routes path) does the job for string->parsed-value, what is the best practice in the other direction? Do I use (m/encode …) directly on the path params?

ingesol21:10:13

To be clear, I would be running this code:

(reitit-frontend/match-by-name routes :view {:tags #{:x :y :z}})

ingesol21:10:03

Expecting this output path:

"/view/x-y-z"
After my custom coercion that encodes to string by joining with “-”

ingesol10:10:01

Bumping this one last time, in case anyone has an answer. I have a working example of using reitit+malli for coercing string path params. Now I want to coerce the other direction. I have my encoder and decoder, but is there an API for coercion in that direction?

ingesol18:10:28

Got this hacky solution working now, doing an initial match-by-name to get access to the schema. This probably isn’t the way to go?

(if-let [schema (-> match :data :parameters :path)]
             (let [schema-instance (m/schema schema {:registry routes-spec/schema-registry})]
               (when-let [explanation (m/explain schema-instance route-params)]
                 (throw (ex-info "Invalid parameters for route creation" explanation)))
               (let [encoded-params (m/encode schema route-params {:registry routes-spec/schema-registry} mt/string-transformer)]
                 (reitit/match-by-name router handler encoded-params)))
             match)

ikitommi16:10:50

I think there should be in-built support for this. About to need it myself. Also, the frontend schemas should be precompiled in router creation for perf. Can't recall if that is the case today. PRs welcome!

ingesol18:10:47

That is the case currently, for coercion in

reitit-frontend/match-by-path
But not in
reitit/match-by-name
Maybe I’m misunderstanding something, but coercion in reitit seems to mean string->something. Maybe that’s correct, converting values to strings isn’t coercion, but rather encoding?

ingesol18:10:02

I do have my schemas precompiled, I just don’t see the reitit api that will use them. And I suspect it doesn’t exist. I will try to see if I can parse the match-by-path code to see how it accesses compiled schemas in the router

hoynk19:10:01

Not sure this is the place for this question, but I am using reitit with the jetty adapter. I start the server straight from the repl. The question is, is there a debug mode that shows stack traces when the code returns a code 500?

hoynk20:10:07

Nevermind, the response contains the stack... my bad! Though it has the jetty stack that happens when the handler returns nil.

hoynk20:10:26

I have another question (I am not sure it is about reitit or jetty) but I created thre tests that basically make a single select in a Cassandra database, the difference between them is that the first was a synchronous ring handler, the second an async one and the third was an async handler using the async mode of the clojure Cassandra lib Alia. The thing is all 3 of them got the same performance (about 2000 req/s in my machine). Can anyone help me understand why there was no difference in performance? Does reitit or jetty automatically handle it somehow?

ingesol10:10:01

Bumping this one last time, in case anyone has an answer. I have a working example of using reitit+malli for coercing string path params. Now I want to coerce the other direction. I have my encoder and decoder, but is there an API for coercion in that direction?

ingesol18:10:28

Got this hacky solution working now, doing an initial match-by-name to get access to the schema. This probably isn’t the way to go?

(if-let [schema (-> match :data :parameters :path)]
             (let [schema-instance (m/schema schema {:registry routes-spec/schema-registry})]
               (when-let [explanation (m/explain schema-instance route-params)]
                 (throw (ex-info "Invalid parameters for route creation" explanation)))
               (let [encoded-params (m/encode schema route-params {:registry routes-spec/schema-registry} mt/string-transformer)]
                 (reitit/match-by-name router handler encoded-params)))
             match)