Sinatra configuring environments on the fly

前端 未结 3 849
离开以前
离开以前 2020-12-08 22:03

I have successfull written a little Sinatra application and already successfully deployed it on heroku.

However I want to run that application in development mode on

相关标签:
3条回答
  • 2020-12-08 22:47

    Look at the Heroku documentation:

    http://devcenter.heroku.com/articles/rack#frameworks

    That's basically what I use for my app, when I start it locally it runs on port 4567.

    0 讨论(0)
  • 2020-12-08 22:49
    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.

    0 讨论(0)
  • 2020-12-08 22:54

    You can also grab ENV['RACK_ENV'] in your config.ru and use that configure your app differently. On Heroku it should run in production by default and if you rackup to fire up your server it will be development by default. Here's some sample code from one of my apps that runs in both environments with the same config file:

    #\ -p 4567
    require 'bundler'               # gem requires
    Bundler.require(:default, ENV['RACK_ENV'].to_sym)  # only loads environment specific gems
    if ENV['RACK_ENV'] == 'production'           # production config / requires
      require './lib/middleware/exceptionmailer'
    
      use Rack::ExceptionMailer, 
        :to => ['me@example.com'],
        :from => 'service@example.com',
        :subject => 'Error Occurred on Rack Application'
    
    else                            # development or testing only
      use Rack::ShowExceptions
    end
    

    This way, Thin or Passenger or whatever will pick it up and the right modules will get loaded in production but you can do other configuration for development.

    0 讨论(0)
提交回复
热议问题