Search code examples
ruby-on-railsngrok

How can I access a subdomain through ngrok?


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  

Solution

  • 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