【发布时间】:2019-08-05 12:18:37
【问题描述】:
在一个演示应用程序中,我们希望处理不同模式中的租户(Postgres 的默认公寓配置)。这些将从应用程序 url 的子域中选择。
我让它工作了,但后来又坏了(它开始使用公共而不是选定的架构)。
我可以将问题缩小到这一点:如果我使用以下代码,一切都会按预期工作:
require 'apartment/elevators/first_subdomain'
Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain
Apartment::Elevators::FirstSubdomain.excluded_subdomains = ['demo', 'localhost']
如果我自己这样做(将丢失的租户行为扩展为返回 404),它就不再起作用了:
class DemoUserSelectionMiddleware < Apartment::Elevators::Generic
def parse_tenant_name(request)
subdomain = request.host.split('.')[0]
unless Rails.application.config.is_demo && subdomain.start_with?('demo_')
'public'
else
subdomain
end
end
def call(*args)
begin
super
rescue Apartment::TenantNotFound
Rails.logger.error "ERROR: Apartment Tenant not found: #{Apartment::Tenant.current.inspect}"
return [404, {"Content-Type" => "application/json"}, ["Error" => "Tenant not found"]]
end
end
end
没有错误,没有日志条目,什么都没有。它只是决定使用“公共”。 使用 byebug 我看到正确找到了子域。
我在这里遗漏了什么明显的东西吗?
【问题讨论】:
标签: ruby-on-rails apartment-gem