Search code examples
ruby-on-railssubdomainstaging

Staging subdomains when application already has subdomains


My current Rails application has the following subdomains:

ui.myapp.com
api.myapp.com

I want to set up a staging environment, and I am wondering what is the best approach in order to set the domains.

Usually, I would do:

staging.myapp.com

But then, how do I access the UI/API sites?

Is it 'ok' to do:

ui.staging.myapp.com
api.staging.myapp.com

?


Solution

  • Assumption: The output from API call is expected in JSON format.

    It will not be advisable to use separate subdomain for functionality like api.staging.myapp.com. You can always use the same url for web application and API Better way would be to use same controller-action from Rails application but instead to return JSON output hash.

    For example to get the users, you will have some code in users_controller.rb as

    class UsersController < ApplicationController
      def index
        @users = User.all
        respond_to do |format|
          format.html do
            render :index
          end
          format.json do
            render :json => {:users => @users}
          end
        end
      end
    end
    

    Now here if the request is html it will render the html page else it will return the json response.

    staging.myapp.com/users will get you the html page of web application displaying users collection and that of staging.myapp.com/users.json will provide you the json response hash of users collection.

    So, you will not need different subdomain to distinguish the api and normal site. But using format of request you can distinguish the functionality.

    I hope this answers your question. If you still have any query feel free to ask.