4

我有一个带有“api”子域的 rails 站点。我本地机器上的路由如下所示:

http://mysite.dev         #<-- normal web stuff
http://api.mysite.dev     #<-- my api

如何映射这两个子域?这是我的 ngrok 配置文件,但 api 端点似乎指向基域。

tunnels:
web:
    subdomain: "my-project"
    proto:
        http: mysite.dev:5000
api:
    subdomain: "api.my-project"
    proto:
        http: api.mysite.dev:5000  
4

1 回答 1

3

如果您在路线中使用约束,我会建议一个约束类,如下所示:

class APIConstraint

  def matches?(request)
     # I would extract the hard coded domains out into some config
     # file, but you get the idea..
     request.host == "ngrok.com" ? request.subdomain.include?("api") : request.subdomain == "api"
  end

end

然后在你的routes.rb

namespace :api do
  constraints APIConstraint.new do
    resources :some_resource
  end
end
于 2015-02-20T20:52:32.117 回答