【问题标题】:Handle subdomains for a Clojure classified web app hosted on Heroku处理 Heroku 上托管的 Clojure 分类 Web 应用程序的子域
【发布时间】:2014-08-03 18:42:01
【问题描述】:

我想在 Heroku 上托管一个分类的 clojure 网络应用程序。该域名已在 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

我该怎么办?

【问题讨论】:

    标签: heroku clojure subdomain ring


    【解决方案1】:

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

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

    (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))
    

    选择适合您要求的。

    【讨论】:

    • 谢谢!虽然无法获得特定于一个子域的路由。这个不行(GET "http://paris.classapp.com/" [] "Welcome to Paris site")(GET "http://newyork.classapp.com/" [] "Welcome to New York site") 是否可以根据特定的子域更改应用的内容?
    • 更新了一些细节的响应。
    • 您提供的第一个解决方案适用于这个小更改:(GET "/" {{host "host"} :headers} (str "Welcome to " host)) 您提供的第二个解决方案将是最好的,但它似乎已损坏。我试过(when-let [route (get-in domain-routes-map [:headers "host"])],但它似乎不起作用!
    • 好收获。进入应该在请求中完成。我已经更新了回复。
    • 出色的答案,非常感谢!最后一件事:defroutes 不接受参数声明,所以你应该删除向量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多