reitit

mjhika 2024-07-19T19:32:05.936639Z

I'm having trouble with ring-handler, idk what i am doing incorrectly here. I keep getting nil from the handler per the docs i expect to be able to run the handler as a fn and get a resolution. Anyone know where i misunderstood? ๐Ÿงต

mjhika 2024-07-19T19:32:31.451679Z

This is what i have tried

(ns dev
  (:require
   [hiccup2.core :as h]
   [reitit.core :as r]
   [reitit.ring :as ring]
   [ring.adapter.jetty9 :as jetty]
   ;; [ring.middleware.reload :as reload]
   [ring.util.response :as resp]))

(defn ping-pong [_]
  (resp/response (str (h/html [:p "pong"]))))

(defn root [r]
  (resp/response (:remote-addr r)))

(def routes
  ["/"
   ["" {:name ::root
        :handler root}]
   ["ping" {:name ::ping
            :handler ping-pong}]]
  #_[["/" {:name ::root
         :handler root}]
   ["/ping" {:name ::ping
             :handler ping-pong}]])

(def router
  (r/router routes))

(comment
  (r/match-by-path router "/ping")
  ;; => #reitit.core.Match{:template "/ping", :data {...}, ...}

  (r/match-by-name router ::ping)
  ;; => #reitit.core.Match{:template "/ping", :data {...}, ...}
  )

(def handler
  (ring/ring-handler router))

(comment
  (handler {:request-method :get :uri "/ping"})
  ;; => nil

  (-> handler
      ring/get-router
      (r/match-by-path "/ping"))
  ;; => #reitit.core.Match{:template "/ping", :data {...}, ...}
  )

2024-07-19T21:31:43.700629Z

you need to use (ring/router routes) instead of (r/router routes) there

mjhika 2024-07-19T21:38:03.614569Z

oh thank you!

๐Ÿ‘ 1
mjhika 2024-07-19T21:38:20.710859Z

that was so simple, thank you for pointing that out