Ruby

Rack, Sinatra and Hanami

Load Envless variables in any Rack app, Sinatra, Hanami or Roda, by requiring envless/env/load first in config.ru.

Any Rack app starts from config.ru, which makes it the one place to load. Require envless/env/load before your app, so the load has happened by the time the app's own files run.

config.ru
require "envless/env/load"
require_relative "app"

run App

Every server that reads config.ru, rackup, Puma, Falcon or Unicorn, now loads first. With Puma's preload_app! the load runs once in the parent and every worker inherits it.

Sinatra

app.rb
require "sinatra/base"
require "envless/env"

class App < Sinatra::Base
  get "/config.json" do
    content_type :json
    Envless.env.client.to_json
  end

  get "/health" do
    Envless.env.key?(:DATABASE_URL) ? "ok" : "missing"
  end
end

Envless.env.client.to_json hands the browser only the variables marked client, so the route can never leak a server secret.

Hanami

Hanami reads its settings from ENV while it boots, so load before it does, at the top of config/app.rb:

config/app.rb
require "envless/env/load"
require "hanami"

module Bookshelf
  class App < Hanami::App
  end
end

Settings defined in config/settings.rb then read the Envless values from ENV, with Hanami's own types and validation on top.

Scripts and workers

A Rake task, a Sidekiq process outside Rails or a one-off script loads the same way: require envless/env/load as the first line, or call Envless::Env.load when the moment matters, and read through Envless.env.

ruby
require "envless/env"

Envless::Env.load
puts Envless.env[:QUEUE_URL]