Mich
Mich

Reputation: 749

Rails routes and regexp

I have a controller in charge of proxying HTTP calls to others services. In my routes.rb I have tried with something like this:

get '/:service/:request', service: /servicefoo|servicebar|serviceomg/,
    request: /(?:(servicefoo|servicebar|serviceomg))(.*)/,
    to: 'proxy#show'

or also like this:

get '/:service/:request', constraints: {
    service: /servicefoo|servicebar|serviceomg/,
    request: /(?:(servicefoo|servicebar|serviceomg))(.*)/
}, to: 'proxy#show'

and also...

get '/:service/:request', service: /passport|tiptop|datacubes/,
    request: /(.*)/,
    to: 'proxy#show'

It seems that the regex is working when I check online: https://regex101.com/r/xX5kP6/3

However within Rails, once I'm in my controller params[:request] only contains admin/users and the rest of the request has been removed... So I can't properly relay the full HTTP request as expected.

What's going on behind the scene?

Thank you.

Upvotes: 0

Views: 835

Answers (1)

Dan Draper
Dan Draper

Reputation: 1121

I'm not sure that the routes support Regexes in the way that you are using them. You can only use Globs (http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments)

You could try reworking your code to use a constraint object:

# 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

Upvotes: 2

Related Questions