reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
dharrigan 2020-08-19T07:03:00.111Z

awesome!

dharrigan 2020-08-19T07:03:03.111200Z

well done! 🙂

ikitommi 2020-08-19T09:58:09.115400Z

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

ikitommi 2020-08-19T09:58:46.116Z

more to learn than in compojure, but mostly no surprises. the docs could be better.

ikitommi 2020-08-19T09:59:56.117400Z

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"}

GGfpc 2020-08-19T15:33:50.120Z

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!)

haywood 2020-08-19T15:55:48.120300Z

you’re using fragments, so try /#/login

haywood 2020-08-19T15:56:01.120500Z

/login is trying to find a server page to render and not getting it

haywood 2020-08-19T15:57:50.121200Z

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

haywood 2020-08-19T15:58:13.121800Z

if you’re using reitit on the server too, you’ll need to set the server side router option {:no-conflicts true}

haywood 2020-08-19T18:28:37.123100Z

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

haywood 2020-08-19T18:29:27.123500Z

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

haywood 2020-08-19T18:29:53.124Z

it seems like we don’t differentiate between query-params and path-params