Handle subdomains for a Clojure classified web app hosted on Heroku Handle subdomains for a Clojure classified web app hosted on Heroku heroku heroku

Handle subdomains for a Clojure classified web app hosted on Heroku


Heroku support wildcard subdomains: https://devcenter.heroku.com/articles/custom-domains#wildcard-domains.

You will have in the host header the original domain, which you can use with something like (completely untested):

(GET "/" {{host "host"} :headers} (str "Welcomed to " host))

You could also create your own routing MW (completely untested):

(defn domain-routing [domain-routes-map]   (fn [req]       (when-let [route (get domain-routes-map (get-in req [:headers "host"]))]           (route req))))

And use it with something like:

 (defroutes paris      (GET "/" [] "I am in Paris")) (defroutes new-new-york    (GET "/" [] "I am in New New York")) (def my-domain-specific-routes     (domain-routing {"paris.example.com" paris "newnewyork.example.com" new-new-york}))

And yet another option is to create a "mod-rewrite" MW that modifies the uri before getting to the Compojure routes:

 (defn subdomain-mw [handler]    (fn [req]        (let [new-path (str (subdomain-from-host (get-in req [:headers "host"])                            "/"                            (:uri req))]             (handler (assoc req :uri new-path))))    (defroutes my-routes        (GET "/:subdomain/" [subdomain] (str "Welcomed to " subdomain))

Pick the one that suits your requirements.