This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-12-01
Channels
- # adventofcode (66)
- # announcements (12)
- # aws (8)
- # babashka (28)
- # beginners (160)
- # cider (28)
- # clara (22)
- # clj-kondo (5)
- # cljdoc (40)
- # clojure (129)
- # clojure-australia (2)
- # clojure-europe (24)
- # clojure-gamedev (19)
- # clojure-nl (5)
- # clojure-norway (15)
- # clojure-sanfrancisco (25)
- # clojure-seattle (2)
- # clojure-spec (13)
- # clojure-uk (29)
- # clojurescript (14)
- # cryogen (5)
- # cursive (7)
- # data-science (1)
- # datascript (5)
- # datomic (8)
- # deps-new (5)
- # emacs (19)
- # events (8)
- # fulcro (32)
- # graalvm (7)
- # helix (9)
- # kaocha (3)
- # lambdaisland (1)
- # london-clojurians (4)
- # malli (5)
- # meander (32)
- # off-topic (143)
- # pathom (4)
- # portal (32)
- # re-frame (7)
- # reagent (33)
- # reitit (2)
- # shadow-cljs (5)
- # spacemacs (4)
- # tools-deps (30)
- # vim (1)
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
.🙌 6