This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-11-03
Channels
- # announcements (2)
- # asami (1)
- # babashka (32)
- # beginners (125)
- # calva (4)
- # cider (1)
- # clj-kondo (16)
- # clj-together (1)
- # cljs-dev (15)
- # clojure (30)
- # clojure-australia (3)
- # clojure-europe (41)
- # clojure-italy (1)
- # clojure-losangeles (1)
- # clojure-nl (4)
- # clojure-spec (68)
- # clojure-uk (28)
- # clojurescript (36)
- # conjure (2)
- # cryogen (1)
- # cursive (2)
- # data-science (2)
- # datascript (2)
- # datomic (70)
- # events (2)
- # fulcro (11)
- # graalvm (1)
- # jobs (4)
- # kaocha (4)
- # leiningen (4)
- # malli (52)
- # meander (21)
- # off-topic (11)
- # pathom (7)
- # pedestal (17)
- # reagent (23)
- # reitit (5)
- # remote-jobs (5)
- # reveal (7)
- # shadow-cljs (24)
- # spacemacs (36)
- # sql (21)
- # vim (18)
- # xtdb (7)
Hello reititians :-) I’ve been searching reitit docs for matching a route with configured a :host
key in its data against the actual window’s host but couldn’t find anything related, I ended up with a custom compiler that I pass as option to reitit.core/router
, like:
(defn host-match-compiler [host]
(reify reitit.trie/TrieCompiler
(linear-matcher [_ matchers _]
(let [size (count matchers)]
(reify reitit.trie/Matcher
(match [_ i max path]
(loop [j 0]
(when (< j size)
(let [m (reitit.trie/match (get matchers j) i max path)]
(or (when (and m (= host (-> m :data :data :host))) m)
(recur (inc j))))))))))
as we’re currenlty using a linear-matcher. Would anyone have a better suggestion than this? how could we move the host matching logic inside the reitit.trie/Matcher
’s match
implementation?@andrea712 excellect question. You could also write an unitlity on top of the router that selects a router based on the host. Something like:
(def host->router
(->> [["/out" {:host "0.0.0.0"}
["/ping"]
["/pong"]]
["/local" {:host "127.0.0.1"}
["/pong"]]]
(r/router)
(r/routes)
(group-by (comp :host second))
(map #(update % 1 r/router))
(into {})))
(-> "0.0.0.0" host->router (r/match-by-path "/out/ping"))
; #reitit.core.Match{:template "/out/ping", :data {:host "0.0.0.0"}, :result nil, :path-params {}, :path "/out/ping"}
(-> "0.0.0.0" host->router (r/match-by-path "/local/ping"))
; => nil