Skip to content

Commit

Permalink
Merge 4b7e83e into f55f043
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jul 3, 2014
2 parents f55f043 + 4b7e83e commit 009cdb2
Show file tree
Hide file tree
Showing 10 changed files with 636 additions and 111 deletions.
12 changes: 6 additions & 6 deletions lib/lotus/cli.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'thor'
require 'lotus/environment'
require 'lotus/commands/server'
require 'lotus/utils/hash'

module Lotus
class Cli < Thor
desc "server", "starts a lotus server"
method_option :port, aliases: '-p', desc: "The port to run the server on, "
desc 'server', 'starts a lotus server'
method_option :port, aliases: '-p', desc: 'The port to run the server on, '
method_option :server, desc: 'choose a specific Rack::Handler, e.g. webrick, thin etc'
method_option :config, desc: 'a rackup configuration file path to load (config.ru)'
method_option :host, desc: 'the host address to bind to'
Expand All @@ -19,14 +19,14 @@ def server
if options[:help]
invoke :help, ['server']
else
Lotus::Commands::Server.new(server_options).start
Lotus::Commands::Server.new(environment).start
end
end

private

def server_options
Lotus::Utils::Hash.new(options).symbolize!
def environment
Lotus::Environment.new(options)
end
end
end
50 changes: 12 additions & 38 deletions lib/lotus/commands/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,27 @@ module Commands
class Server < ::Rack::Server
attr_reader :options

def initialize(options)
@options = fix_rack_options default_options.merge(options)
def initialize(env)
@options = _extract_options(env)
end

# Primarily this removes the ::Rack::Chunked middleware
# which is the cause of Safari content-length bugs.
def middleware
m = Hash.new { |e, m| e[m] = [] }
m["deployment"].concat([::Rack::ContentLength, ::Rack::CommonLogger])
m["development"].concat(m["deployment"] + [::Rack::ShowExceptions, ::Rack::Lint])
m
mw = Hash.new { |e, m| e[m] = [] }
mw["deployment"].concat([::Rack::ContentLength, ::Rack::CommonLogger])
mw["development"].concat(mw["deployment"] + [::Rack::ShowExceptions, ::Rack::Lint])
mw
end

private

def default_options
{
environment: environment,
pid: nil,
port: 2300,
host: default_host,
accesslog: [],
config: "config.ru"
}
end

def environment
ENV['RACK_ENV'] || 'development'
end

def default_host
environment == 'development' ? 'localhost' : '0.0.0.0'
end

# Frustratingly, some of racks options are capitalized
# this maps between our command line options and the correct
# rack options.
def fix_rack_options(opts)
opts_map = {
:port => :Port,
:host => :Host,
:accesslog => :AccessLog
}

fixed_opts = Hash.new
opts.each { |k,v| fixed_opts[opts_map.fetch(k, k)] = v }
fixed_opts
def _extract_options(env)
env.to_options.merge(
Host: env.host,
Port: env.port,
AccessLog: []
)
end
end
end
Expand Down
22 changes: 8 additions & 14 deletions lib/lotus/configuration.rb
Original file line number Diff line number Diff line change
@@ -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,18 +639,14 @@ def host(value = nil)
if value
@host = value
else
@host ||= 'localhost'
@host ||= @env.host
end
end

# The URI port for this application.
# This is used by the router helpers to generate absolute URLs.
#
# By default this value is `80`, if `scheme` is `"http"`, or `443` if
# `scheme` is `"https"`.
#
# This is optional, you should set this value only if your application
# listens on a port not listed above.
# By default this value is `2300`.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
Expand All @@ -675,29 +673,25 @@ def host(value = nil)
# end
# end
#
# Bookshelf::Application.configuration.port # => 80
# Bookshelf::Application.configuration.port # => 2300
#
# @example Setting the value
# require 'lotus'
#
# module Bookshelf
# class Application < Lotus::Application
# configure do
# port 2323
# port 8080
# end
# end
# end
#
# Bookshelf::Application.configuration.port # => 2323
# Bookshelf::Application.configuration.port # => 8080
def port(value = nil)
if value
@port = Integer(value)
else
@port ||
case scheme
when 'http' then 80
when 'https' then 443
end
@port || @env.port
end
end

Expand Down
63 changes: 63 additions & 0 deletions lib/lotus/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'thread'
require 'lotus/utils/hash'

module Lotus
class Environment
RACK_ENV = 'RACK_ENV'.freeze
LOTUS_ENV = 'LOTUS_ENV'.freeze
DEFAULT_ENV = 'development'.freeze

LOTUS_HOST = 'LOTUS_HOST'.freeze
DEFAULT_HOST = 'localhost'.freeze
LISTEN_ALL_HOST = '0.0.0.0'.freeze

LOTUS_PORT = 'LOTUS_PORT'.freeze
DEFAULT_PORT = 2300

DEFAULT_CONFIG = 'config.ru'.freeze

def initialize(options = {})
@options = Utils::Hash.new(options).symbolize!.freeze
@mutex = Mutex.new
@mutex.synchronize { set_env_vars! }
end

def environment
@environment ||= ENV[LOTUS_ENV] || ENV[RACK_ENV] || DEFAULT_ENV
end

def host
@host ||= @options.fetch(:host) {
ENV[LOTUS_HOST] || default_host
}
end

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

def config
@options.fetch(:config) { DEFAULT_CONFIG }
end

def to_options
@options.merge(
environment: environment,
config: config,
host: host,
port: port
)
end

private
def set_env_vars!
ENV[LOTUS_ENV] = ENV[RACK_ENV] = environment
ENV[LOTUS_HOST] = host
ENV[LOTUS_PORT] = port.to_s
end

def default_host
environment == DEFAULT_ENV ? DEFAULT_HOST : LISTEN_ALL_HOST
end
end
end

0 comments on commit 009cdb2

Please sign in to comment.