This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-02-02
Channels
- # announcements (4)
- # babashka (1)
- # beginners (4)
- # cider (4)
- # clerk (3)
- # clojure (25)
- # clojure-brasil (5)
- # clojure-europe (6)
- # clojure-nl (1)
- # clojure-norway (48)
- # clojure-uk (4)
- # clojuredesign-podcast (3)
- # clojurescript (1)
- # conjure (1)
- # emacs (23)
- # hyperfiddle (3)
- # jobs (6)
- # lsp (12)
- # off-topic (5)
- # polylith (13)
- # reagent (8)
- # reitit (18)
- # sci (20)
- # shadow-cljs (8)
- # specter (3)
- # squint (3)
Hi! is there anything in reitit related to http://pedestal.io/pedestal/0.7/reference/table-syntax.html with this I mean I want to do the same thing, which is have multiple host for different set of routes?
(ns myapp.service
(:require [io.pedestal.http.route :as route]))
(def application-routes
(route/expand-routes
#{{:host "example.com" :scheme :https}
["/user" :get user-search-form]
["/user/:user-id" :get view-user :constraints {:user-id #"[0-9]+"}]
,,,
}))
At my company, we use Compojure and have roughly 300 endpoints, not to mention the endpoints with multiple methods associated. This works but is pretty slow and hard to edit. I'm considering transitioning us to use reitit, but it's going to take time to implement and deploy, so there will have to be a period of "using both". Does anyone have tips for supporting both while performing such a transition?
Similar situation here. Except all the routes need to be on the same swagger page, so having them under two routing frameworks seems impossible. I prototyped a layer of macros that expand to reitit routes, it might be interesting to look at for now, eventually I'd like to make it its own library. It would be especially cool to have a tool that could then macroexpand-1 all these compatibility-bridge macros and magically turn the code into reitit. But that's just an idea for now. https://github.com/threatgrid/ctia/pull/1403
The actual compojure-api->reitit bridging macros are in src/ctia/lib/compojure/api/core_reitit.clj
https://github.com/threatgrid/ctia/pull/1403/files#diff-c44fa66bbfa3254a08bfbe3f09e1175371728bd59739c2299f4d491ff7e22a01
i don't have access to those channels, but i'll check that out
The tests explain the approach better test/ctia/lib/compojure/api/core_reitit_test.clj
https://github.com/threatgrid/ctia/pull/1403/files#diff-1c3acd943e1aeb074115b5d6103c7598f3b18fc524575be5c640d529512cf554
For example
(is (= '["/my-route" {:get {:handler (clojure.core/fn [req__0] (clojure.core/let [] (do {:status 200})))}}]
(dexpand-1
`(sut/GET "/my-route" []
{:status 200}))))
Couple of things to keep in mind before embarking.
1. You can't have routing logic based on any part of the request other than the uri or request-method, e.g. (context "foo" req (cond (get req ...) ...)
2. You won't necessarily get the full speedup of a reitit-first app, since you might have route conflicts https://cljdoc.org/d/metosin/reitit/0.7.0-alpha7/doc/basics/route-conflicts#:~:text=When%20a%20Router%20is%20created,thrown%20with%20a%20descriptive%20message.
But also, compojure-api can be incredibly slow if you use context
, so it's worth trying. I got a 600% speedup in CI runs by changing (context "" (GET ))
to (let [r (GET ))] (context "" r))
.
That was also programmatically achieved. Might be worth trying first to get a taste of potential speedups, here's how I did it https://github.com/threatgrid/ctia/commit/d5c24198238145d0b387ef9d905b56982108a66b
Though this is with compojure-api 1.x. There might be better optimizations on the 2.x branch.
Oh, and I'm still working out what the equivalent to compojure.api.api/api
is in reitit. You might be using a similar batteries-included way of starting the app.
good discussion. I have migrated few c-api project to reitit. reitit definitely needs the battery-packs, currently copy-pasting the good practice boilerplate from project to another. I have also drafted an functional layer of top of reitit, with ideas picked out from frameworks from other languages. One missing concept in reitit is a module
, which would tie together different aspects (routes, data, middleware, schemas) and could be used as a simple abstraction to build up things fast, “web server with api-module”. Opinions and scattered config can be hided an an unified module configuration (backed by malli) - you should be able to write c-api “api” as a module. Has been in the works for 5 years, might be done in a week or in a year. Maybe there should be a open brainstorm of this?
c-api + reitit sounds really interesting, looking forward to that.
One way you could migrate incrementally is by creating a reitit ring handler and specifying the old compojure handler as the default handler, then start migrating routes to the new router
Be careful about sessions: https://github.com/ferdinand-beyer/reitit-ring-defaults#warning-on-session-middleware