This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-20
Channels
- # architecture (25)
- # beginners (68)
- # cider (10)
- # clara (3)
- # cljs-dev (90)
- # cljsrn (16)
- # clojure (132)
- # clojure-austin (7)
- # clojure-berlin (3)
- # clojure-czech (1)
- # clojure-dusseldorf (1)
- # clojure-greece (5)
- # clojure-italy (39)
- # clojure-spec (5)
- # clojure-uk (78)
- # clojured (2)
- # clojurescript (92)
- # community-development (6)
- # cursive (7)
- # data-science (1)
- # datascript (14)
- # datomic (32)
- # duct (8)
- # emacs (5)
- # figwheel (3)
- # fulcro (47)
- # hoplon (12)
- # jobs (10)
- # luminus (16)
- # lumo (5)
- # off-topic (1)
- # onyx (2)
- # parinfer (47)
- # pedestal (6)
- # re-frame (10)
- # reagent (2)
- # reitit (61)
- # ring (8)
- # ring-swagger (16)
- # shadow-cljs (116)
- # sql (17)
- # utah-clojurians (2)
- # vim (1)
hi, i've been reading the documentation and i must say the library looks nice and the documentation is very torough
in my own (very much less well thought out) routing library i accept vars as sub-routes
and resolve these in real time ...is this possible at all with all the transformation you're performing on the route tree ?
@carkh if you don’t want to use the reloaded, you need to re-create the router for all requests, as it compiles the routes to be fast - and use Vars. Something like:
(defn app [request]
((ring/ring-handler (ring/ring-router [#'routes1 #'routes2])) request))
Shouldn’t use that in prod thou, might take some microseconds to compile it at demand.
the route conflict resolution is also happening at router creation-time, a good reason to re-create the router after any route changes.
The thing is that I'm doing pretty large projects ... I like to have my routes follow the code and make a tree that spans over many namespaces
one of these projects is as old as clojure 1.0 ... there was no reloaded workflow back then
that's not usually a problem, but when i recompile a file, i'd love to see the changes =)
before that i was using common lisp ...and i tried having a single route file that woudl dispatch to other parts of the code ... but that get unwieldy after a while
(defn create-router [routes reload?]
(if reload? #(r/router routes) (constantly routes)))
(def router (create-router ["/foo" #'routes] true))
(r/match-by-path (router) "/foo/bar")
Just one router at the top, the partial routes can be normal route structure, e.g. (def routes [“/inc” ::inc])
I'm not sure that's necessary ... I've been able to do what I wanted without vars ... but your solution only works for one level deep
maybe something like a segment router might be forced upon seeing a var and then resolve the var on the fly ?
@carkh answered, realised it should work with functions. As a bonus you can create routes programmatically based on function parameters. e.g. pass in context
of some sort and modify routes based on that.
i think there's no need to make the top router (prod-router and dev-router) functions, we're needlessly creating an object each time
great! I think the constantly
creates the instance once and reuses that after that. So, it’s only done once. e.g. (constantly (fire-missiles!))
is a bad idea 🙂
(def prod-router (create-router routes false))
=> it creates the closure once. It’s an extra function call in prod, but that should be a nanosec.
(defn slow2 []
(Thread/sleep 2000)
2)
;; slow
(def a (constantly (slow2)))
;; nanos
(a)
yes, but in your example we were calling constantly ...constantly ! we came to the same conclusion, use it at top level
the docs are under /docs
and the README has the guide to run gitbook if you have time to create a page for that, under advanced
maybe?