【问题标题】:Rack apps mounted in different subdomains安装在不同子域中的机架应用程序
【发布时间】:2013-11-04 03:51:36
【问题描述】:

我正在与 Sinatra 一起构建 Grape API。到目前为止,我一直在像这样将它们安装在不同的路线中:

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

“/api”由 Grape 应用提供服务,而“/”由 Sinatra 应用提供服务。但我想使用子域来分隔这些问题,而不是实际的“子 URL”。有关如何执行此操作的任何线索?

提前感谢您的帮助。

【问题讨论】:

标签: ruby sinatra rack grape


【解决方案1】:

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

你也可以自己动手:

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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2011-09-23
    • 2011-01-11
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多