4

I have a rails site with an "api" subdomain. The routes on my local machine look like this:

http://mysite.dev         #<-- normal web stuff
http://api.mysite.dev     #<-- my api

How can I map these two subdomains? This is my ngrok config file, but the api endpoint seems to point to the base domain.

tunnels:
web:
    subdomain: "my-project"
    proto:
        http: mysite.dev:5000
api:
    subdomain: "api.my-project"
    proto:
        http: api.mysite.dev:5000  
rob
  • 4,069
  • 3
  • 34
  • 41
  • 2
    I don't think it would be possible without avoiding some updates to your code. As the resulting ngrok URL would be `api.my-project.ngrok.com` Would be helpful if you can share your routes file, or share how you are routing requests based on the subdomain? – Jasdeep Singh Feb 20 '15 at 20:40

1 Answers1

3

In case you are using Constraints in your routes, I would suggest a constraint class such as the following:

class APIConstraint

  def matches?(request)
     # I would extract the hard coded domains out into some config
     # file, but you get the idea..
     request.host == "ngrok.com" ? request.subdomain.include?("api") : request.subdomain == "api"
  end

end

And then in your routes.rb

namespace :api do
  constraints APIConstraint.new do
    resources :some_resource
  end
end
Jasdeep Singh
  • 3,276
  • 4
  • 28
  • 47