Fork me on GitHub
#reitit
<
2020-08-19
>
dharrigan07:08:03

well done! 🙂

ikitommi09:08:09

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

ikitommi09:08:46

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

ikitommi09:08:56

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

GGfpc15:08:50

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

haywood15:08:48

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

haywood15:08:01

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

haywood15:08:50

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

haywood15:08:13

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

haywood18:08:37

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

haywood18:08:53

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