Fork me on GitHub
#reitit
<
2022-03-18
>
craftybones14:03:19

Hello. I’d like an interceptor to bail to a different handler with a 4xx response if a certain spec is not met. I can obviously do this with a simple s/valid? check inside the interceptor. However, is there any other way to do this in reitit?

socksy14:03:11

lots of people have compared bidi and reitit, but I'm curious if anyone's combined them. We have most of our HTTP server routes in bidi, and instead of doing the work in one go to replace bidi with reitit, it'd be really nice to be able to specify a whole bunch of new routes using reitit. Anyone tried anything like this, or am I bonkers?

juhoteperi15:03:51

Both create ring handler functions, taking ring request map and returning response map. You can combine them by calling (or (bidi req) (reitit req)) or such. BUT some middlewares are side-effecting, like reading the request body input-stream, so you need to take some care to avoid these side-effects if the first handler doesn't return response. When/if bidi reads the request body stream, reitit (muuntaja) can't read it. If you put reitit first it might work because reitit IIRC only runs the middlewares if we find handler for given request.

juhoteperi15:03:41

Or you could mount reitit handler inside bidi routes, or the other way.

1
wombawomba15:03:08

Is there a router other than linear-router that I can use (in CLJS) to handle conflicting paths? I have a rather small number of routes so lookup performance shouldn't be a concern, and linear-router is quite costly in terms of emitted JS size and initialization time (seemingly because it uses reitit.trie).

juhoteperi15:03:45

Not right now, I think we had a issue or PR testing minimal router to optimize the artifact size.

juhoteperi15:03:55

If you also use reitit.coercion.malli (or the others), it also includes bunch of unnecessary swagger stuff on Cljs side: https://github.com/metosin/reitit/issues/463

wombawomba15:03:42

Alright. I do use schemas but I've stripped that out of the route definitions in the frontend.

wombawomba15:03:36

I'm thinking perhaps I could write my own router?

wombawomba15:03:05

Are there any built-in utility functions that I can use to 'brute-force' matches?

wombawomba15:03:26

I'd be happy to just go down the list of routes one by one and use the first match

juhoteperi15:03:59

I think you can take linear-router and remove references to trie-router

juhoteperi15:03:31

Or hm, does the linear-router always use trie...

juhoteperi15:03:59

I can't find any issue about this now, but we have talked about this

wombawomba15:03:25

I'll just steal that code 🙂 thanks

wombawomba15:03:59

BTW, if you're looking at improving JS performance, I noticed that I could cut router startup time in half by moving the calls to impl/resolve-routes, impl/compile-routes, impl/conflicting-paths and impl/path-conflicting-routes out into a separate function and calling it in a macro (i.e. at compile time)

wombawomba15:03:08

Starting the router is reasonably fast out of the box (~100ms on my computer), but for e.g. mobile users speeding it up can make a noticeable difference

juhoteperi15:03:05

Macro compilation time only works if the route tree is completely static value. But it could be useful to provide macro for those cases.

juhoteperi15:03:03

Using simple router instead of building a trie could also make the startup faster

wombawomba15:03:06

You wouldn't need to provide a macro for it; if you just refactored the code so that users could call the 'precomputable' parts first and then build a router from those, then it'd be easy for users to write their own macro to handle it

juhoteperi15:03:40

Hmm yeah separating static and dynamic parts could be nice way to solve that.

juhoteperi15:03:03

I still don't know. Many parts, like conflicting-paths check etc. would need to be done again when combining those parts. It would get quite complex quickly.