处理在Heroku上托管的Clojure分类Web应用程序的子域

leo*_*bot 5 subdomain clojure heroku ring

我有一个分类的clojure网络应用程序,我想在Heroku上托管.该域名在Godaddy注册.

拥有多个子域的最有效和最有效的方法是什么:

  • newyork.classapp.com
  • montreal.classapp.com
  • paris.classapp.com
  • ...

用户,所有逻辑,应该跨子域共享,所以我希望只有一个代码库.

将子域重定向到第一级文件夹很容易,如下所示: paris.classapp.com- >classapp.com/paris/

但我希望用户在浏览网站时继续看到子域名,如下所示: paris.classapp.com/cars/blue-car-to-sell

与此相反:classapp.com/paris/cars/blue-car-to-sell

我该怎么办?

Dan*_*ero 3

Heroku 支持通配符子域:https://devcenter.heroku.com/articles/custom-domains#wildcard-domains

您将在主机标头中包含原始域,您可以将其与类似的内容一起使用(完全未经测试):

(GET "/" {{host "host"} :headers} (str "Welcomed to " host))
Run Code Online (Sandbox Code Playgroud)

您还可以创建自己的路由MW(完全未经测试):

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

并将其与以下内容一起使用:

 (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}))
Run Code Online (Sandbox Code Playgroud)

还有另一个选择是创建一个“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))
Run Code Online (Sandbox Code Playgroud)

选择适合您要求的一款。