创建新租户公寓 Gem - 总是重新启动?
Creating New Tenant Apartment Gem - Always restart?
我有一个 rails 4.2 多租户应用程序,它使用公寓 gem,非常棒。
每个公司都有自己的子域。我正在使用自定义 "elevator",它查看完整的请求主机以确定应加载哪个 "Tenant"。
当我创建一家新公司时,我有一个 after_create 挂钩来使用正确的请求主机创建新租户。
这似乎总是需要在开发和生产中重新启动服务器,否则我会收到“找不到租户”错误。
它在开发中使用 sqlite,在生产中使用 postgres。
每次创建新租户时我真的必须重新启动服务器吗?有没有自动的方法来做到这一点?也许只需重新加载初始化程序即可,但我不确定该怎么做 that/if 这可能吗?
我已经为此纠结了一个月,一直未能找到可行的解决方案。请帮忙!
initializers/apartment.rb
require 'apartment/elevators/host_hash'
config.tenant_names = lambda { Company.pluck :request_host }
Rails.application.config.middleware.use 'Apartment::Elevators::HostHash', Company.full_hosts_hash
initializers/host_hash.rb
require 'apartment/elevators/generic'
module Apartment
module Elevators
class HostHash < Generic
def initialize(app, hash = {}, processor = nil)
super app, processor
@hash = hash
end
def parse_tenant_name(request)
if request.host.split('.').first == "www"
nil
else
raise TenantNotFound,
"Cannot find tenant for host #{request.host}" unless @hash.has_key?(request.host)
@hash[request.host]
end
end
end
end
end
公司模式
after_create :create_tenant
def self.full_hosts_hash
Company.all.inject(Hash.new) do |hash, company|
hash[company.request_host] = company.request_host
hash
end
end
private
def create_tenant
Apartment::Tenant.create(request_host)
end
什么最终起作用了
我更改了电梯配置以远离公寓 gem 中的 HostHash,并使用了完全自定义的。主要基于公寓的问题 gem github: https://github.com/influitive/apartment/issues/280
initializers/apartment.rb
Rails.application.config.middleware.use 'BaseSite::BaseElevator'
app/middleware/base_site.rb
require 'apartment/elevators/generic'
module BaseSite
class BaseElevator < Apartment::Elevators::Generic
def parse_tenant_name(request)
company = Company.find_by_request_host(request.host)
return company.request_host unless company.nil?
fail StandardError, "No website found at #{request.host} not found"
end
end
end
我认为问题可能在于您的 host_hash.rb 位于初始化程序目录中。根据您在评论中引用的 Apartment gem ReadME,它不应该位于名为 "middleware" 的文件夹中吗?在那个例子中,他们使用了app/middleware/my_custom_elevator.rb。也许你的看起来像 app/middleware/host_hash.rb?
现在文件在 initializers 中,所以它是从那里加载的。但是您的 apartment.rb 通过 Rails.application.config.middleware.use
引用了它。只是一种预感,但除了最初加载它之外,它可能还在一个不存在的中间件文件夹中寻找它。我会继续创建 app/middleware,将文件放在那里,然后看看会发生什么。不确定,但您可能还需要更改 require
路径。
如果有帮助请告诉我们。
我有一个 rails 4.2 多租户应用程序,它使用公寓 gem,非常棒。
每个公司都有自己的子域。我正在使用自定义 "elevator",它查看完整的请求主机以确定应加载哪个 "Tenant"。
当我创建一家新公司时,我有一个 after_create 挂钩来使用正确的请求主机创建新租户。
这似乎总是需要在开发和生产中重新启动服务器,否则我会收到“找不到租户”错误。
它在开发中使用 sqlite,在生产中使用 postgres。
每次创建新租户时我真的必须重新启动服务器吗?有没有自动的方法来做到这一点?也许只需重新加载初始化程序即可,但我不确定该怎么做 that/if 这可能吗?
我已经为此纠结了一个月,一直未能找到可行的解决方案。请帮忙!
initializers/apartment.rb
require 'apartment/elevators/host_hash'
config.tenant_names = lambda { Company.pluck :request_host }
Rails.application.config.middleware.use 'Apartment::Elevators::HostHash', Company.full_hosts_hash
initializers/host_hash.rb
require 'apartment/elevators/generic'
module Apartment
module Elevators
class HostHash < Generic
def initialize(app, hash = {}, processor = nil)
super app, processor
@hash = hash
end
def parse_tenant_name(request)
if request.host.split('.').first == "www"
nil
else
raise TenantNotFound,
"Cannot find tenant for host #{request.host}" unless @hash.has_key?(request.host)
@hash[request.host]
end
end
end
end
end
公司模式
after_create :create_tenant
def self.full_hosts_hash
Company.all.inject(Hash.new) do |hash, company|
hash[company.request_host] = company.request_host
hash
end
end
private
def create_tenant
Apartment::Tenant.create(request_host)
end
什么最终起作用了
我更改了电梯配置以远离公寓 gem 中的 HostHash,并使用了完全自定义的。主要基于公寓的问题 gem github: https://github.com/influitive/apartment/issues/280
initializers/apartment.rb
Rails.application.config.middleware.use 'BaseSite::BaseElevator'
app/middleware/base_site.rb
require 'apartment/elevators/generic'
module BaseSite
class BaseElevator < Apartment::Elevators::Generic
def parse_tenant_name(request)
company = Company.find_by_request_host(request.host)
return company.request_host unless company.nil?
fail StandardError, "No website found at #{request.host} not found"
end
end
end
我认为问题可能在于您的 host_hash.rb 位于初始化程序目录中。根据您在评论中引用的 Apartment gem ReadME,它不应该位于名为 "middleware" 的文件夹中吗?在那个例子中,他们使用了app/middleware/my_custom_elevator.rb。也许你的看起来像 app/middleware/host_hash.rb?
现在文件在 initializers 中,所以它是从那里加载的。但是您的 apartment.rb 通过 Rails.application.config.middleware.use
引用了它。只是一种预感,但除了最初加载它之外,它可能还在一个不存在的中间件文件夹中寻找它。我会继续创建 app/middleware,将文件放在那里,然后看看会发生什么。不确定,但您可能还需要更改 require
路径。
如果有帮助请告诉我们。