This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-27
Channels
- # adventofcode (1)
- # announcements (4)
- # beginners (120)
- # calva (5)
- # cider (12)
- # clara (3)
- # cljdoc (48)
- # cljs-dev (33)
- # cljsrn (4)
- # clojure (124)
- # clojure-dev (43)
- # clojure-europe (2)
- # clojure-italy (168)
- # clojure-nl (2)
- # clojure-spec (7)
- # clojure-uk (79)
- # clojurescript (50)
- # core-logic (6)
- # cursive (12)
- # datascript (1)
- # datomic (8)
- # devcards (2)
- # emacs (5)
- # events (2)
- # figwheel-main (6)
- # fulcro (18)
- # graphql (42)
- # hyperfiddle (3)
- # jobs (1)
- # luminus (2)
- # nrepl (5)
- # off-topic (59)
- # onyx (5)
- # parinfer (2)
- # pathom (10)
- # pedestal (2)
- # portkey (3)
- # re-frame (24)
- # reagent (6)
- # reitit (54)
- # remote-jobs (1)
- # ring (5)
- # shadow-cljs (75)
- # spacemacs (35)
- # sql (22)
- # tools-deps (16)
- # unrepl (10)
(defn debug-session
[{:keys [session] :as req}]
(let [new-uuid (java.util.UUID/randomUUID)
session-id (get-in req [:cookies "ring-session"])]
{:status 200 :body (str session "\n"
new-uuid "<br />\n"
session-id "<br />\n"
ring.middleware.session.memory/memory-store)
:session (assoc session :foo new-uuid)}))
(def handler
(ring/ring-handler
(ring/router
[["/hoge" {:get debug-session}]
["/fuga" {:get debug-session}]]
{:data {:middleware [#(wrap-defaults debug-session site-defaults)]}})))
with these routesbut as soon as i load the other route the session is ignored and a new session id is generated
stripping out routing
(def handler
(wrap-defaults debug-session site-defaults))
it works@vale I think it should be
(def handler
(ring/ring-handler
(ring/router
[["/hoge" {:get debug-session}]
["/fuga" {:get debug-session}]]
{:data {:middleware [[wrap-defaults site-defaults]]}})))
it works correctly with
(def handler
(ring.middleware.session/wrap-session debug-session))
@vale should not be, it's just a stateless router. Busy today, could investigate tomorrow if you write an issue with sample code.
route-unique, oh, that! you should create one memory-store and give it as a reference to the mw.
This should be highlighted in the docs. With Interceptors, it's ok as they can be chained as values, mws are HOFs and need to be created per endpoint.
actually yesterday i had a very similar problem where we tried to with-redef a middleware wrapper and it didn't work as expected
Hello, I have a classic resource creation where routes distinguish only by HTTP verbs, not path :
["/events"
["" {:name :events-index
:get c/events-index}]
["" {:name :events-create
:post c/events-create}]]
I get a conflict error on creating the router : Router contains conflicting route paths
Is it something feasible with reitit?
ThanksI found this in the documentation : https://metosin.github.io/reitit/ring/ring.html#request-method-based-routing On seeing the example
(def app
(ring/ring-handler
(ring/router
[["/all" handler]
["/ping" {:name ::ping
:get handler
:post handler}]])))
it doesn't seem possible to separate two routes with two name and two verbs from a single path.
I feel like it would be nice to be able to do this so that it would be possible to have RESTful routes à la Rails : https://guides.rubyonrails.org/routing.html#crud-verbs-and-actions.
Or am I misunderstanding the way to use reitit?
Thanks@dam you can disable the conflict resolution with a router option {:conflicts nil}
, it fallbacks to linear router and the first matching servers the request.
@ikitommi thanks Does it mean that the general performance of the router goes from ~ O(1) to O(n)?
yes, and currently for all routes. there could be a router algo splits the routes into conflicting & non-conflicting, and uses linear-router for the latter and best possible for the first one.
seen cases where there are 100+ routes and two legacy-endpoints conflicting. would be 98% fast still…
also, :name
should be unique for the paths by design, so one needs to have a new identifier for the ring/http endpoints, could be something like :id
?
(def app
(ring/ring-handler
(ring/router
[["/all" handler]
["/ping" {:name ::ping
:get {:id ::get, :handler get-thing}
:post {:id ::post, :handler add-thing}]])))
if one want’s to identify the path + method pair…. (in reverse-routing). haven’t needed that myself
What I really like with reitit is that I have a "one source of truth" for routes with a hiccup-like syntax (more readable than bidi (for me)).
Thanks for the information and this great library that I was looking for for quite some time.
With exactly the following features: - Data driven route definition (à la Hiccup, even better) - Route + name + handler at the same place - Bi-directional routing (for path generation in templates) - AND last but not the least, middleware application on whole sub-path of routes (necessary for api + frontend)
Oh and to answer your question with the id, I would see something less nested like so:
(def app
(ring/ring-handler
(ring/router
[["/all" handler]
["/en"
["/ping"
["" {:name ::ping-get
:language :en
:method :get
:handler get-thing}]
["" {:name ::ping-add
:language :en
:method :post
:handler add-thing}]]]
["/fr"
["/ping"
["" {:name ::ping-get
:language :fr
:method :get
:handler get-thing}]
["" {:name ::ping-add
:language :fr
:method :post
:handler add-thing}]]]])))
This comes from the use cases I have.
The unique identifier of a route would be the set of values: #{path name language method}
With path
mandatory and :get
as default for method
.We have a similar use-case where we define a set of actions (commands and queries) as data and generate both routes for reitit and web-socket handler for eines. As reitit is just data, it's easy to generate the route tree from a domain data definitions. And it's easy to mix generated routes and traditional routes into a same app.
reitit accepts also nested sequences, so it's easy to loop the domain definitions and emit a sequence of routes.