awesome!
well done! 🙂
key points that the docs could highlight for middleware are: • each endpoint has it’s own chain of middleware, accumulated from route data • supports default ring-middeware, middleware as data/maps and vector-syntax
more to learn than in compojure, but mostly no surprises. the docs could be better.
here’s hopefully a good example on using and inspecting the chain:
(require '[reitit.core :as r])
(require '[reitit.ring :as ring])
(defn mw [id]
{:name id
:wrap (fn [handler]
(fn [request]
(prn id)
(handler request)))})
(def handler (constantly {:status 200, :body "pong"}))
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [(mw "api")]}
["/ping" {:middleware [(mw "ping")]
:get {:middleware [(mw "ping-get")]
:handler handler}}]]
{:data {:middleware [(mw "root")]}})))
(app {:request-method :get, :uri "/api/ping"})
;"root"
;"api"
;"ping"
;"ping-get"
;=> {:status 200, :body "pong"}
(->> app ring/get-router r/compiled-routes)
; ... lots of stuff in
(require '[reitit.middleware :as rm])
(-> app
ring/get-router
r/compiled-routes
(get-in [0 2 :get :data :middleware]))
;[{:name "root", :wrap #object[user$mw]}
; {:name "api", :wrap #object[user$mw]}
; {:name "ping", :wrap #object[user$mw]}
; {:name "ping-get", :wrap #object[user$mw]}]
(rm/chain *1 handler)
; => composed chain
(*1 {:request-method :get, :uri "/api/ping"})
;"root"
;"api"
;"ping"
;"ping-get"
;=> {:status 200, :body "pong"}
I need some help with reitit-frontend I have this code that is supposed to load a reagent component when I go to "/login". However, connecting to "localhost:9700/login" I get a figwheel error saying "Figwheel Server: Resource not found". On the other hand, If I set the route to "/" and load "localhost:9700" the component loads successfully. Does anyone know what I'm doing wrong?
(defonce match (ratom/atom nil))
(defn current-page []
[:div
[:ul
[:li [:a {:href (rfe/href ::frontpage)} "Frontpage"]]]]
(if @match
(let [view (:view (:data @match))]
[view @match])))
(def routes
[["/login"
{:name ::frontpage
:view login/component}]])
(defn init! []
(rfe/start!
(rf/router routes {})
(fn [m] (reset! match m))
;; set to false to enable HistoryAPI
{:use-fragment true})
(rdom/render [current-page] (.getElementById js/document "app")))
(init!)
you’re using fragments, so try /#/login
/login is trying to find a server page to render and not getting it
if you want to use the history api, you need to create a catch all route in the server route definitions that delegates unknown routes to the app template
if you’re using reitit on the server too, you’ll need to set the server side router option {:no-conflicts true}
I have route that has a path param like (rf/dispatch [:route.name {:url "cool/url/here"}])
, I’d like for the final path to not be url encoded, but I’m not sure how to achieve that
it goes through to (rfe/push-state
, which ends up here https://github.com/metosin/reitit/blob/577447dc2321d36d5a4f439b3d6486e8e5b3e004/modules/reitit-core/src/reitit/impl.cljc#L240-L243
it seems like we don’t differentiate between query-params and path-params