Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Rack or Sinatra based environment configuration utility?

Is there anything in the Sinatra / Rack world similar to Rails configuration loading scheme that loads one of the config\enviroments\*.rb files depending on Rails.env

I know I could develop one pretty easily, i was just wondering if there was something already in place.

like image 628
Daniel Beardsley Avatar asked Dec 09 '22 12:12

Daniel Beardsley


2 Answers

If you're following the Rails convention of putting a file for each environment in config/environments/environment_name.rb, you can put something like this in your Sinatra app, or for Rack in your config.ru file:

Dir.glob(File.dirname(__FILE__) + "/config/environments/#{settings.environment}.rb", &method(:require))

With some minor modifications you could make it load other file locations/combinations. Sinatra's configure blocks work just as well, too.

like image 57
Jarrod Avatar answered Apr 28 '23 23:04

Jarrod


It turns out that there is something from Sinatra, that provides a similar, though limited, functionality.

See the code: https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1120

So that you can do this:

class MyApp < Sinatra::Base
  configure :development, :test do
    #only executes this code when environment is equal to one of the passed arguments
    # I'm pretty sure Sinatra sets this based on ENV['RACK_ENV']
  end
end
like image 40
Daniel Beardsley Avatar answered Apr 28 '23 21:04

Daniel Beardsley