How does Sinatra know which environment to use? - ruby

I uploaded a Sinatra app to the server (heroku). But it seems like the app acts itself like it's at a localhost unlike my another Rails app which works well there.
So how do I check if my Sinatra app uses the correct environment or not? And how does Sinatra know which environment to use?

By nature heroku will take care of setting the environment. By default it's "production". In case you have different config/behavior for different use case, you would have to code that first.
For example
if ENV=="production"
# do something
elsif ENV=="staging"
# do something else
end
I am not sure why would you want to set environment explicitly to "production" or something else. That should be left at discretion of hosting environment.
Update
More info on Heroku documentation
Further update
heroku run printenv
above should list environment variables.

I add an environment variable to all my heroku instances:
heroku config:add APP_NAME=<myappname>
Then, for Sinatra, I have the following in the config.ru:
# detect environments and setup some passwords
case ENV['APP_NAME']
when 'prod-damon'
# whatever for production
when 'dev-damon'
# whatever for development on Heroku
else
# whatever for local
end

Related

Setting environment variables for a Sinatra app on Heroku

I have a Heroku-hosted Sinatra app for which I need to enable Rack::Protection::AuthenticityToken. To this end, I need to set up an environment variable to enable session cookies:
use Rack::Session::Cookie, secret: ENV['MY_APP_SECRET']
I need the cookies to work both on localhost and on Heroku. Given that it's bad practice to hard-code the variable in one's config.ru like this:
use Rack::Session::Cookie, secret: 123qwerty
...do I set the variable in my local .bash_profile using this syntax:
export MY_APP_SECRET=123qwerty
...or do I set it on the Heroku CLI with:
heroku config:set MY_APP_SECRET=123qwerty ?
You'll need to set the environment variable for both local development and for Heroku. For local development, you could set it in your .bash_profile, but that would make it available to every process in your shell. Instead you could set it whenever you run your local server like this:
MY_APP_SECRET=123qwerty shotgun config.ru
A better alternative, IMO, is to use a tool like the dotenv Ruby gem to manage environment variables.
Either way, you still need to set the variable for the Heroku environment using the heroku config:set command.

Where to put API keys for Twitter app on server

I'm currently writing a couple Twitter bots for my friends using the Twitter gem for Ruby. My plan was to store the keys for them in a .txt file with the rest of the bot's code on my server, but everything I've read has said the keys shouldn't be readable within the code. Is this secure enough, and if not what would be a good solution? Thanks!
A common approach is to save the environment variables into a file called .env that is ignored by version control (and therefore won't be included on Github) but read by the code. One gem to help with this is dotenv.
add .env to the .gitignore file.
create a local .env file with all your env vars
require 'dotenv' and put Dotenv.load somewhere at the beginning of your script. In Rails, the require is unnecessary and you can place the load call in any file in the config/initializers folder
Check that your app works fine locally. The environment variables should be found in the ENV hash from Ruby code.
Save changes and push new version of app to digital ocean
manually create the .env file on the digital ocean server, in the root of the repo
run digital ocean server and check that everything works.
other notes:
see How To Read and Set Environmental and Shell Variables on a Linux VPS
some platforms like heroku have a different mechanism for setting environment variables, such as heroku config:set or web UIs.
You can set environment variables on a one-off basis using the env command in bash, for example:
env a=hello b=' world' ruby -e 'puts ENV["a"] + ENV["b"]'
# => hello world
This can give a quick way to configure a program without getting into argument parsing. For example in Rails, you can say rails c test to open a console using the test environment, but env RAILS_ENV=test rails c should do the same thing.

Unicorn not reading ENV variables

I'm having some issues with SECRET_KEY_BASE setting on our production Ubuntu server with Unicorn and nginx. I added the variable to .bashrc and its reading fine when I try echo $SECRET_KEY_BASE, but for some reason I'm getting the following error:
app error: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` (RuntimeError)
Also, when I try to access the variable with ENV["SECRET_KEY_BASE"] from within the console in production environment, I can read it fine.
I tried restarting Unicorn and server and it doesn't help. Any idea why this is happening?
You may need to add the variables to the unicorn.conf file, since it seems you are launching Unicorn with a different user or without sourcing your .bashrc.
In any case, I suggest you use dotenv gem to store your env variables in a .env file within your project. That is the cleanest way I know. If you need to have capistrano integration and handle all the different environments with ease, you can also use dotenv-deployment gem.

Heroku is switching my play framework 2 config file

I have a Play! application which is on Heroku.
My config file is different between my local application and the same on Heroku. Especially for the URL of my MongoDB base.
On localhost my base address is 127.0.0.1 and on heroku it's on MongoHQ. So when I push my application to Heroku I modify my config file.
But some times, like this morning Heroku change the config file. I pushed my application correctly configured on Heroku this morning and everything worked until now.
When I watch the logs I see that Heroku changed my config and try to connect to my local MongoDB base.
Is someone knowing what ? I hope I'm clear :)
Thanks everybody !
If there are differences in your application in different environments (e.g. local vs production), you should be using assigning the values with environment variables. For Play apps, you can use environment variables in your application.conf file, like this:
`mongo.url=${MONGO_URL}`
Then, on Heroku you can set the environment variables with config vars, like this (note, this may already be assigned for you by the add-on provider):
$ heroku config:add MONGO_URL=...
Locally, you can use Foreman to run your application with the environment variables stored in an .env file in your project root.

Adding ruby code to script/rails that only executes in rails development environment

I am using Rails 3 and have a need to run WEBrick with SSL support during development. To achieve this, I've followed this guide:
http://www.nearinfinity.com/blogs/chris_rohr/configuring_webrick_to_use_ssl.html
This works well, however, I want to ensure that these settings do no affect my rails application when run in production mode. We are currently using Apache/Passenger, and the project appears to still run fine. Is there a clean way, however, to make sure that this code isn't even executed? I'm thinking a possible answer could be an if/end block around the code, or perhaps a built-in rails facility that allows development-only code to be placed in a separate file or something similar.
Looks like ENV['RAILS_ENV'] is your friend. The ENV hash shows you the Unix environment the app is being run under and Rails itself will look at RAILS_ENV to decide in which mode to run. You could do something like this:
if ENV['RAILS_ENV'].to_s == 'development' || ENV['RAILS_ENV'].to_s == ''
# do your thing here
end
You can also make sure that you run webrick with that environment:
#> RAILS_ENV=development /path/to/webrick/script
Hope it helps.

Resources