我有一个控制器负责代理对其他服务的HTTP调用。在我的(?s)
中,我尝试过这样的事情:
routes.rb
或者也是这样:
get '/:service/:request', service: /servicefoo|servicebar|serviceomg/,
request: /(?:(servicefoo|servicebar|serviceomg))(.*)/,
to: 'proxy#show'
还有...
get '/:service/:request', constraints: {
service: /servicefoo|servicebar|serviceomg/,
request: /(?:(servicefoo|servicebar|serviceomg))(.*)/
}, to: 'proxy#show'
当我在线查看时,似乎正则表达式正在运行:https://regex101.com/r/xX5kP6/3
然而,在Rails中,一旦我在我的控制器get '/:service/:request', service: /passport|tiptop|datacubes/,
request: /(.*)/,
to: 'proxy#show'
中只包含params[:request]
,其余请求已被删除......所以我无法正确转发正常的HTTP请求。
幕后发生了什么?
谢谢。
答案 0 :(得分:2)
我不确定这些路由是否以您使用它们的方式支持Regex。您只能使用Globs(http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments)
您可以尝试重新编写代码以使用约束对象:
# From the Rails guides
class BlacklistConstraint
def initialize
@ips = Blacklist.retrieve_ips
end
def matches?(request)
@ips.include?(request.remote_ip)
end
end
Rails.application.routes.draw do
get '*path', to: 'blacklist#index',
constraints: BlacklistConstraint.new
end