How to get the selflink in a compojure handler? How to get the selflink in a compojure handler? json json

How to get the selflink in a compojure handler?


The Ring request map contains all the necessary information to construct a "selflink". Specifically, :scheme, :server-name, :server-port, and :uri values can be assembled into full request URL. When I faced this problem I created Ring middleware that adds the assembled request URL to the Ring request map. I could then use the request URL in my handlers as long as I pass the request map (or some subset of it) into the handler. The following snippet shows one way of implementing this:

(defroutes app-routes  (GET "/myhome/:id" [id :as {:keys [self-link]}] (home-page id self-link))  (route/resources "/")  (route/not-found "Not Found"))(defn wrap-request-add-self-link [handler]  (fn add-self-link [{:keys [scheme server-name server-port uri] :as r}]    (let [link (str (name scheme) "://" server-name ":" server-port uri)]      (handler (assoc r :self-link link)))))(def app  (-> app-routes    handler/site    wrap-request-add-self-link))