diff --git a/bin/annyong b/bin/annyong index 498868a..ad8d03f 100755 --- a/bin/annyong +++ b/bin/annyong @@ -8,33 +8,5 @@ require "optparse" require "rack" require "annyong" -options = { - :port => 9292, - :host => "0.0.0.0" -} -opts = OptionParser.new do |opts| - opts.banner = "Usage: annyong [options] - -Options: - " - opts.on("--port [PORT]", "The port to use (default: 9292)") do |port| - options[:port] = port - end - - opts.on("--host [HOST]", "The host to listen on (default: 0.0.0.0)") do |host| - options[:host] = host - end - - #opts.on("--path [PATH]", "The directory to serve") do |path| - #options[:path] = path - #end - -end -opts.parse! - -Rack::Server.start({ - :config => File.join(File.dirname(__FILE__), "../assets/rack/config.ru"), - :Port => options[:port], - :Host => options[:host], - :AccessLog => [], -}) +application = Annyong::Application.new(ARGV) +application.run diff --git a/lib/annyong.rb b/lib/annyong.rb index 9d36ad1..380281d 100644 --- a/lib/annyong.rb +++ b/lib/annyong.rb @@ -1,6 +1,7 @@ module Annyong VERSION = '0.3' autoload :Directory, "annyong/directory" + require "annyong/application" end unless "".respond_to?(:each) diff --git a/lib/annyong/application.rb b/lib/annyong/application.rb new file mode 100644 index 0000000..29200e9 --- /dev/null +++ b/lib/annyong/application.rb @@ -0,0 +1,66 @@ +require 'ostruct' + +module Annyong + + class Application + + def server + @server ||= Rack::Server.new({ + :config => File.join(File.dirname(__FILE__), "../../assets/rack/config.ru"), + :Port => port, + :Host => host, + :AccessLog => [], + }) + end + + def run + server.start + end + + def port + options[:port] + end + + def host + options[:host] + end + + def initialize(args) + @options = parse_options(args) + end + + def options + @options ||= parse_options + end + + private + + def parse_options(args) + options = { + :port => 9292, + :host => "0.0.0.0" + } + + option_parser = OptionParser.new do |opts| + opts.banner = "Usage: annyong [options]" + opts.separator "" + opts.separator "Options:" + + opts.on("--port [PORT]", "The port to use (default: #{options[:port]})") do |port| + options[:port] = port + end + opts.on("--host [HOST]", "The host to listen on (default: #{options[:host]})") do |host| + options[:host] = host + end + + #opts.on("--path [PATH]", "The directory to serve") do |path| + #options[:path] = path + #end + end + + option_parser.parse!(args) + options + end + + end +end