Navigation Menu

Skip to content

Commit

Permalink
Let Lotus::Configuration to set the proper host/port according to the…
Browse files Browse the repository at this point in the history
… command line args that we pass to 'lotus server'
  • Loading branch information
jodosha committed Jul 3, 2014
1 parent 616a083 commit 3bfd300
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 6 deletions.
15 changes: 13 additions & 2 deletions lib/lotus/configuration.rb
@@ -1,4 +1,5 @@
require 'lotus/utils/kernel'
require 'lotus/environment'
require 'lotus/config/load_paths'
require 'lotus/config/assets'
require 'lotus/config/routes'
Expand All @@ -17,6 +18,7 @@ class Configuration
# @api private
def initialize
@blk = Proc.new{}
@env = Environment.new
end

# Set a block yield when the configuration will be loaded
Expand Down Expand Up @@ -637,7 +639,7 @@ def host(value = nil)
if value
@host = value
else
@host ||= 'localhost'
@host ||= @env.host
end
end

Expand Down Expand Up @@ -693,7 +695,16 @@ def port(value = nil)
if value
@port = Integer(value)
else
@port ||
# FIXME this hack MUST be removed when we implement the multi-environment feature
#
# when @port is set:
# it always takes the precedence
# when @port isn't set:
# development:
# @env.port should take the precedence
# other env:
# the scheme should take the precedence
@port || ((@env.port != 2300) ? @env.port : nil) ||
case scheme
when 'http' then 80
when 'https' then 443
Expand Down
7 changes: 4 additions & 3 deletions lib/lotus/environment.rb
Expand Up @@ -15,7 +15,7 @@ class Environment

DEFAULT_CONFIG = 'config.ru'.freeze

def initialize(options)
def initialize(options = {})
@options = Utils::Hash.new(options).symbolize!.freeze
set_env_vars!
end
Expand All @@ -26,12 +26,13 @@ def environment

def host
@options.fetch(:host) {
environment == DEFAULT_ENV ? DEFAULT_HOST : LISTEN_ALL_HOST
ENV[LOTUS_HOST] ||
( environment == DEFAULT_ENV ? DEFAULT_HOST : LISTEN_ALL_HOST )
}
end

def port
@options.fetch(:port) { DEFAULT_PORT }
@options.fetch(:port) { ENV[LOTUS_PORT] || DEFAULT_PORT }.to_i
end

def config
Expand Down
2 changes: 1 addition & 1 deletion test/commands/server_test.rb
Expand Up @@ -20,7 +20,7 @@
let(:env) { ENV['RACK_ENV'] || "development" }

it 'sets the options correctly for rack' do
@server.options[:Port].must_equal "3005"
@server.options[:Port].must_equal 3005
@server.options[:Host].must_equal "example.com"
end

Expand Down
38 changes: 38 additions & 0 deletions test/configuration_test.rb
Expand Up @@ -6,6 +6,9 @@
module MockApp
end

ENV['LOTUS_HOST'] = nil
ENV['LOTUS_PORT'] = nil

@namespace = MockApp
@configuration = Lotus::Configuration.new
end
Expand Down Expand Up @@ -323,12 +326,32 @@ def to_pathname
end

describe '#host' do
before do
ENV['LOTUS_HOST'] = nil
ENV['LOTUS_ENV'] = nil
end

describe "when not previously set" do
before do
@configuration = Lotus::Configuration.new
end

it 'defaults to a specific value' do
@configuration.host.must_equal 'localhost'
end
end

describe "when the env var is set" do
before do
ENV['LOTUS_HOST'] = 'lotustest.org'
@configuration = Lotus::Configuration.new
end

it 'returns that value' do
@configuration.host.must_equal 'lotustest.org'
end
end

describe "when called with an argument" do
it 'sets the value' do
@configuration.host(host = 'lotusrb.org')
Expand Down Expand Up @@ -356,6 +379,21 @@ def to_pathname
end
end

describe "when the env var is set" do
before do
ENV['LOTUS_PORT'] = '2306'
@configuration = Lotus::Configuration.new
end

after do
ENV['LOTUS_PORT'] = nil
end

it 'returns that value' do
@configuration.port.must_equal 2306
end
end

describe "when called with an argument" do
it 'sets the value' do
@configuration.port(port = '8080')
Expand Down
172 changes: 172 additions & 0 deletions test/environment_test.rb
@@ -0,0 +1,172 @@
require 'test_helper'

describe Lotus::Environment do
before do
ENV['LOTUS_ENV'] = nil
ENV['RACK_ENV'] = nil
ENV['LOTUS_HOST'] = nil
ENV['LOTUS_PORT'] = nil
end

describe '#environment' do
describe "when LOTUS_ENV is set" do
before do
ENV['LOTUS_ENV'] = 'test'
@env = Lotus::Environment.new
end

it 'returns that value' do
@env.environment.must_equal 'test'
end
end

describe "when RACK_ENV is set" do
before do
ENV['RACK_ENV'] = 'production'
@env = Lotus::Environment.new
end

it 'returns that value' do
@env.environment.must_equal 'production'
end
end

describe "when none is set" do
before do
@env = Lotus::Environment.new
end

it 'defaults to "development"' do
@env.environment.must_equal 'development'
end
end

describe "when all are set" do
before do
ENV['LOTUS_ENV'] = 'test'
ENV['RACK_ENV'] = 'production'
@env = Lotus::Environment.new
end

it 'gives the precedence to LOTUS_ENV' do
@env.environment.must_equal 'test'
end
end
end

describe '#host' do
describe "when the correspoding option is set" do
before do
@env = Lotus::Environment.new(host: 'lotusrb.test')
end

it 'returns that value' do
@env.host.must_equal 'lotusrb.test'
end
end

describe "when the corresponding option isn't set" do
describe "and LOTUS_HOST is set" do
before do
ENV['LOTUS_HOST'] = 'lotus.host'
@env = Lotus::Environment.new
end

it 'returns that value' do
@env.host.must_equal 'lotus.host'
end
end

describe "and the current environment is the default one" do
before do
@env = Lotus::Environment.new
end

it 'returns localhost' do
@env.host.must_equal 'localhost'
end
end

describe "and the current environment isn't the default" do
before do
ENV['LOTUS_ENV'] = 'staging'
@env = Lotus::Environment.new
end

it 'returns 0.0.0.0' do
@env.host.must_equal '0.0.0.0'
end
end

describe "and all the other env vars are set" do
before do
ENV['LOTUS_HOST'] = 'lotushost.test'
ENV['LOTUS_ENV'] = 'test'
@env = Lotus::Environment.new
end

it 'gives the precedence to LOTUS_HOST' do
@env.host.must_equal 'lotushost.test'
end
end
end

describe "when the corresponding option and all the other env vars are set" do
before do
ENV['LOTUS_HOST'] = 'lotushost.test'
ENV['LOTUS_ENV'] = 'test'
@env = Lotus::Environment.new(host: 'lotusrb.org')
end

it 'gives the precedence to the option' do
@env.host.must_equal 'lotusrb.org'
end
end
end

describe '#port' do
describe "when the correspoding option is set" do
before do
@env = Lotus::Environment.new(port: 1234)
end

it 'returns that value' do
@env.port.must_equal 1234
end
end

describe "when the corresponding option isn't set" do
describe "and LOTUS_PORT is set" do
before do
ENV['LOTUS_PORT'] = '3244'
@env = Lotus::Environment.new
end

it 'returns that value' do
@env.port.must_equal 3244
end
end

describe "and no env vars are set" do
before do
@env = Lotus::Environment.new
end

it 'defaults to 2300' do
@env.port.must_equal 2300
end
end
end

describe "when the corresponding option and all the other env vars are set" do
before do
ENV['LOTUS_PORT'] = '8206'
@env = Lotus::Environment.new(port: 2323)
end

it 'gives the precedence to the option' do
@env.port.must_equal 2323
end
end
end
end

0 comments on commit 3bfd300

Please sign in to comment.