Skip to content
timblair edited this page May 28, 2012 · 5 revisions

Configuration

Goliath provides a simple mechanism to share and load common configuration between different environments and even different web-services. To get started, simply create a config directory and provide a ruby configuration file with the same name as your API:

      |- echo.rb
      |- config/
        |- echo.rb

The config files themselves are Ruby code that will be evaluated when the server is starting. During this process, config and options variables are accessible from within your config file. Anything stored in the config will be available to all subsequent requests: shared connections, shared variables, and so on. The options hash represents the command line options provided to the API at startup.

As an example, in our echo.rb configuration file we could initialize a pool of asynchronous MySQL connections:

require 'em-synchrony/mysql2'

config['db'] = EM::Synchrony::ConnectionPool.new(:size => 20) do
    ::Mysql2::EM::Client.new(:hostname => 'localhost', :username => 'test',
                             :password => 'password', :socket => nil,
                             :database => 'echo_db',
                             :reconnect => true)
end

Then, in the API server itself we can access one of the shared DB connections directly:

ret = db.query("SELECT COUNT(1) FROM some_table")

Environments

The running environment can be set in a number of ways. In order of increasing precedence:

  1. Default (:development)
  2. RACK_ENV
  3. -e/--environment NAME flag from the command line
  4. Explicit call to Goliath#env=

You can customize your configuration based on the type of environment:

hostname = nil

environment :production do
    hostname = 'production.example.org'
end

environment :development do
    hostname = 'dev.example.org'
end

environment :test do
    hostname = 'test.example.org'
end

Shared config data

Often, you will need or want to share the same configuration between multiple web-services. To do so, simply import the common configuration file:

import('shared')

# regular configuration ...

environment :production do
    # ...
end