Skip to content

Commit

Permalink
print local URL and shutdown command when starting server
Browse files Browse the repository at this point in the history
additionally, move default bind addr and port to constants
  • Loading branch information
mojavelinux committed Sep 13, 2013
1 parent 408ac09 commit c0de793
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/awestruct/cli/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Awestruct
module CLI
class Generate

def initialize(config, profile=nil, base_url=nil, default_base_url='http://localhost:4242', force=false)
def initialize(config, profile=nil, base_url=nil, default_base_url=Options::DEFAULT_BASE_URL, force=false)
@profile = profile
@base_url = base_url
@default_base_url = default_base_url
Expand Down
2 changes: 1 addition & 1 deletion lib/awestruct/cli/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def invoke_force()
end

def invoke_generate()
@success = Awestruct::CLI::Generate.new( config, options.profile, options.base_url, 'http://localhost:4242', options.force ).run
@success = Awestruct::CLI::Generate.new( config, options.profile, options.base_url, Options::DEFAULT_BASE_URL, options.force ).run
end

def invoke_deploy()
Expand Down
2 changes: 1 addition & 1 deletion lib/awestruct/cli/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def finalize(opts={})
rake
then visit your site at: http://localhost:4242
then visit your site at: #{::Awestruct::CLI::Options::DEFAULT_BASE_URL}
END
end
end
Expand Down
22 changes: 16 additions & 6 deletions lib/awestruct/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ module Awestruct
module CLI

class Options
LOCAL_HOSTS = {
'localhost' => 'localhost',
'0.0.0.0' => 'localhost',
'127.0.0.1' => 'localhost',
'::1' => '[::1]',
'localhost6' => 'localhost6'
}
DEFAULT_BIND_ADDR = '0.0.0.0'
DEFAULT_PORT = 4242
DEFAULT_BASE_URL = %(http://#{LOCAL_HOSTS[DEFAULT_BIND_ADDR] || DEFAULT_BIND_ADDR}:#{DEFAULT_PORT})

attr_accessor :generate
attr_accessor :server
Expand All @@ -27,8 +37,8 @@ class Options
def initialize()
@generate = nil
@server = false
@port = 4242
@bind_addr = '0.0.0.0'
@port = DEFAULT_PORT
@bind_addr = DEFAULT_BIND_ADDR
@auto = false
@force = false
@init = false
Expand Down Expand Up @@ -71,10 +81,10 @@ def parse!(args)
opts.on( '-u', '--url URL', 'Set site.base_url' ) do |url|
self.base_url = url
end
opts.on( '-d', '--dev', 'Run site in development mode (--auto, --server, --port 4242 and --profile development)' ) do |url|
opts.on( '-d', '--dev', "Run site in development mode (--auto, --server, --port #{DEFAULT_PORT} and --profile development)" ) do |url|
self.server = true
self.auto = true
self.port = 4242
self.port = DEFAULT_PORT
self.profile = 'development'
end

Expand All @@ -90,10 +100,10 @@ def parse!(args)
opts.on( '-a', '--auto', 'Auto-generate when changes are noticed' ) do |a|
self.auto = a
end
opts.on( '-p', '--port PORT', Integer, 'Server port (default: 4242)' ) do |port|
opts.on( '-p', '--port PORT', Integer, "Server port (default: #{DEFAULT_PORT})" ) do |port|
self.port = port
end
opts.on( '-b', '--bind ADDR', 'Server address (default: 0.0.0.0)' ) do |bind_addr|
opts.on( '-b', '--bind ADDR', "Server address (default: #{DEFAULT_BIND_ADDR})" ) do |bind_addr|
self.bind_addr = bind_addr
end
opts.on( '-g', '--[no-]generate', 'Generate site' ) do |g|
Expand Down
5 changes: 4 additions & 1 deletion lib/awestruct/cli/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ module CLI
class Server
attr_reader :server

def initialize(path, bind_addr='0.0.0.0', port=4242)
def initialize(path, bind_addr=Options::DEFAULT_BIND_ADDR, port=Options::DEFAULT_PORT)
@path = path
@bind_addr = bind_addr
@port = port
end

def run
url = %(http://#{Options::LOCAL_HOSTS[@bind_addr] || @bind_addr}:#{@port})
msg = %(Starting preview server at #{url} (Press Ctrl-C to shutdown))
puts %(#{'*' * msg.length}\n#{msg}\n#{'*' * msg.length}\n)
::Rack::Server::start( :app => Awestruct::Rack::App.new( @path ),
:Port => @port,
:Host => @bind_addr
Expand Down
1 change: 0 additions & 1 deletion lib/awestruct/frameworks/base_Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ def run_awestruct(args, opts = {})
opts[:spawn] ||= true
end

msg 'Starting the local preview server...(Press Ctrl-C to shutdown)'
puts "Running command: #{cmd}"
if opts[:spawn]
pid = spawn cmd
Expand Down

0 comments on commit c0de793

Please sign in to comment.