2013-10-25 26 views
3

我建立一个葡萄API一起西纳特拉。到目前为止,我已经安装在他们不同的途径是这样的:机架安装的应用程序在不同的子域

run Rack::URLMap.new("/" => Frontend::Server.new, 
        "/api" => API::Server.new) 

其中“/ API”是由葡萄的应用投放和“/”由西纳特拉的应用程序。但我想用子域分隔这些问题,而不是实际的“子URL”。如何做到这一点的任何线索?

在此先感谢您的帮助。

+0

http://stackoverflow.com/a/19591172/1279355这可能对您的其他解决方案 –

回答

0

有一个rack-subdomain宝石,但是它只能处理重定向路径,而不是架的应用程序。您可以将其分叉并将其重定向到机架应用程序。

你也可以只推出自己:

class SubdomainDispatcher 
    def initialize 
    @frontend = Frontend::Server.new 
    @api  = API::Server.new 
    end 

    def call(env) 
    if subdomain == 'api' 
     return @api.call(env) 
    else 
     return @frontend.call(env) 
    end 
    end 

    private 

    # If may be more robust to use a 3rd party plugin to extract the subdomain 
    # e.g ActionDispatch::Http::URL.extract_subdomain(@env['HTTP_HOST']) 
    def subdomain 
    @env['HTTP_HOST'].split('.').first 
    end 
end 


run SubdomainDispatcher.new 
相关问题