reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
idkfa 2020-12-01T13:56:41.381Z

Thanks to a suggestion by @seancorfield to use a middleware function for this I came up with the following which works. Excuse the beginner-level Clojure 😅

;; middleware.clj

(def api-subdomain-to-path
  {:name    ::api-subdomain-to-path
   :compile (fn [_ _]
              (fn [handler _]
                (fn [req]
                  (if (str/starts-with? (:server-name req) "api.")
                    (handler (assoc req :uri (str "/api" (:uri req))))
                    (handler req)))))})
;; core.clj

(ring/ring-handler
  (ring/router routes
    {:exception pretty/exception
     :data      {:middleware [...]})
  (ring/routes ...)
  {:middleware [[middleware/api-subdomain-to-path :api-subdomain-to-path]]}))
At first I had this new middleware function as first in the middleware vector in the ring/router data map, and while this did update the uri in the request the routing wasn't working. I then realised there was a different place for top-level middleware, which is given to the ring-handler.

2🙌
dharrigan 2020-12-01T14:53:56.381300Z

that's cool