2

I'm writting a simple web app (sinatra) thats does github authentication.

I have to make a create a link with a callback param, somethig like: https://github.com/login/oauth/authorize?client_id=b5b1a33df1c7b5acccac&redirect_uri=http://localhost:4567/step2callback&scope=public_repo,user,gist,admin:repo_hook,gist, so that when the uses click on it it will authenticate, authorize my application, then send the user back to my web app.

Since i'm using rspec, I would like to make a code that pass on tests and on production, so I would like to get the current host+port to use it on the code I generate the link, something like:

HOST = get_current_host # this is the problem, how to get it?
@authorize_url = @client.authorize_url(@client.client_id,
                                      {
                                      :redirect_uri => "#{HOST}/step2", 
                                      :scope => 'public_repo,user,gist,admin:repo_hook,gist'
                                      })

So, my question is how do I get my current running host on heroku, so my code would work on test and production?

Eduardo Santana
  • 5,780
  • 3
  • 19
  • 21

1 Answers1

0

You can configure different values for different environments

A simple example given below:

# File: test.rb
require 'sinatra'

# Set the values of various host names based on deployment environment
configure(:production) { set :host, "production-host" }
configure(:development) { set :host, "development-host" }
configure(:test) { set :host, "test-host" }

get '/' do
  "Hi #{settings.host}"
end

Set RACK_ENV environment variable to environment value - production, development, or test

>set RACK_ENV=test

Run the Sinatra App

>ruby test.rb
[2015-12-17 20:20:46] INFO  WEBrick 1.3.1
[2015-12-17 20:20:46] INFO  ruby 2.1.7 (2015-08-18) [x64-mingw32]
== Sinatra (v1.4.6) has taken the stage on 4567 for test with backup from WEBrick
[2015-12-17 20:20:46] INFO  WEBrick::HTTPServer#start: pid=11040 port=4567

Access the URL:

>curl http://localhost:4567
Hi test-host

If you are using Rack to run your Sinatra app, you could have following as config.ru

# config.ru
require 'sinatra'
require './test.rb'

configure(:production) { set :host, "production-host" }
configure(:development) { set :host, "development-host" }
configure(:test) { set :host, "test-host" }

run Sinatra::Application

In this case, test.rb will be simplified like below:

# test.rb
require 'sinatra'

get '/' do
  "Hi #{settings.host}"
end

You can specify the environment on command line as shown below:

> rackup -E test config.ru

This answer is inspired from Sinatra configuring environments on the fly

Community
  • 1
  • 1
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • Hi @WandMaker thanks for the replay, it will help. But it doesn't show how to get the current host from heroku. Let me explain, suppose I create a *new heroku host and and deploy it there*, then I will have to configure the variable again. The log from heroku show us the current host, I was thinking if it there's a way to read the current host the same way the app that create the log does. – Eduardo Santana Dec 19 '15 at 17:56
  • Or this http://stackoverflow.com/questions/6016783/sinatra-how-do-i-get-the-servers-domain-name – Wand Maker Dec 19 '15 at 18:04