我有一个分类的 clojure 网络应用程序,我想在 Heroku 上托管。该域名已在 Godaddy 注册。
拥有多个子域的最有效和最有效的方法是什么:
用户,所有逻辑,都应该跨子域共享,所以我希望只有一个代码库。
将子域重定向到第一级文件夹很容易,如下所示:
paris.classapp.com
-> classapp.com/paris/
但我希望用户在浏览网站时继续看到子域,如下所示:
paris.classapp.com/cars/blue-car-to-sell
与此相反:
classapp.com/paris/cars/blue-car-to-sell
我该怎么办?
最佳答案
Heroku 支持通配符子域:https://devcenter.heroku.com/articles/custom-domains#wildcard-domains .
您将在主机 header 中拥有原始域,您可以将其用于(完全未经测试)之类的内容:
(GET "/" {{host "host"} :headers} (str "Welcomed to " host))
您还可以创建自己的路由 MW(完全未经测试):
(defn domain-routing [domain-routes-map]
(fn [req]
(when-let [route (get domain-routes-map (get-in req [:headers "host"]))]
(route req))))
并将其与以下内容一起使用:
(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}))
还有一个选择是创建一个“mod-rewrite”MW,在到达 Compojure 路由之前修改 uri:
(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))
选择一款适合您的要求。
关于heroku - 处理托管在 Heroku 上的 Clojure 分类 Web 应用程序的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25107872/