Sinatra configuring environments on the fly
C:\>type tmp.ru
require 'sinatra'
configure(:production){ p "I'm production" }
configure(:development){ p "I'mma dev mode" }
configure(:sassycustom){ p "I'mma own mode" }
exit!
C:\>rackup tmp.ru
"I'mma dev mode"
C:\>rackup -E development tmp.ru
"I'mma dev mode"
C:\>rackup -E production tmp.ru
"I'm production"
C:\>rackup -E sassycustom tmp.ru
"I'mma own mode"
C:\>rackup -E notdefined tmp.ru
If you don't specify an environment, development
is used by default. You can specify any environment name you want, though 'production' is very common. If you specify an environment that you don't configure, no configuration block will match. (It might be a mistake on your part, but it's not an error caught by the code.)
Note that the Sinatra documentation says that setting RACK_ENV environment variable will be used if available. This used to not work, but some time in the last few years it has been fixed!
If, for example, you can set an environment variable for your service, you can control the mode.
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.
Ruby Sinatra configured to work on production and development
According to Sinatra Documentation:
Environments can be set through the RACK_ENV environment variable. The
default value is "development". In the "development" environment all
templates are reloaded between requests, and special not_found and
error handlers display stack traces in your browser. In the
"production" and "test" environments, templates are cached by default.To run different environments, set the RACK_ENV environment variable:
RACK_ENV=production ruby my_app.rb
You also can use the development?
and production?
methods to change the logic:
get '/api/test' do
if settings.development?
return "It is dev"
else if settings.production?
return "It is PROD"
end
end
If settings.development?
doesn't work, you can try Sinatra::Application.environment == :development
In Sinatra, what is the difference between production and development environment?
Basically, only templates are affected by the env var.
You are free to implement every other application logic methods based on current env:
if settings.production?
# this will only run in production
else
# every other env
end
You can also do something like this when need conditional configs:
configure :production, :development do
enable :logging
end
Cannot access sinatra app through the local network
Try ruby app.rb -o 0.0.0.0
or ruby app.rb -e production
. Either should work.
Web server on wireless interface?
You are going about it wrong.
If you invoke your server with the -h
flag, Sinatra will spit out its help:
Usage: server [options]
-p port set the port (default is 4567)
-o addr set the host (default is 0.0.0.0)
-e env set the environment (default is development)
-s server specify rack server/handler (default is thin)
-x turn on the mutex lock (default is off)
The -e env
tells Sinatra to use a specific environment.
If I remember right, the problem is tied to the environment Sinatra, and Rack, think you're running in, "development", "testing" or "production". "Sinatra configuring environments on the fly" has some important information for you, as does Sinatra's "Configuring Settings" doc.
Passing options to rackup via a Sinatra application
You're actully going to pass options to thin on the command line directly or via a configuration file. See all options:
$ thin -h
For production, use a configuration file:
$ thin -C thin-production.yml -R config.ru start
Here is an example thin-production.yml file:
---
address: localhost
port: 3020
servers: 4
max_conns: 1024
max_persistent_conns: 512
timeout: 30
environment: production
pid: tmp/pids/thin-production.pid
log: log/thin-production.log
daemonize: true
Starting Sinatra Web Application on Windows
Use thin or some other webserver:
gem install thin
Then just pass in the rack config file(config.ru):
$ thin -R config.ru start
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
the port in this case is 3000, point browser to 0.0.0.0:3000
Related Topics
Ruby: Creating a Sandboxed Eval
Nested Forms in Rails - Accessing Attribute in Has_Many Relation
How to Have Rspec Test for My Default Scope
Ruby-Debug19 on Ruby-1.9.3-Preview1
Functional Code Examples in Ruby
Using $ Sudo Bundle Exec ... Raises 'Bundle: Command Not Found' Error
How to Insert Video Youtube API V3 Through Service Account with Ruby
How to Get Ruby to Print a Full Backtrace Instead of a Truncated One
Name of This Month (Date.Today.Month as Name)
How Does Ruby's Sort_By {Rand} Work
The Compiler Failed to Generate an Executable File. (Runtimeerror)
How to Create a Nokogiri Case Insensitive Xpath Selector
Has Anyone Successfully Deployed a Rails Project with Ruby 1.9.1
How to Send Message Using Gmail API with Ruby Google API Client