From 3749da0e8cc99f476c224d09c0e3dda7b314f8a8 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Sun, 25 Oct 2015 22:56:27 +0900 Subject: add cli --- lib/plum/rack/cli.rb | 125 ++++++++++++++++++++++++++++++++++++++++++++++ lib/plum/rack/config.rb | 6 ++- lib/plum/rack/listener.rb | 23 +++++++-- 3 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 lib/plum/rack/cli.rb (limited to 'lib/plum/rack') diff --git a/lib/plum/rack/cli.rb b/lib/plum/rack/cli.rb new file mode 100644 index 0000000..967764f --- /dev/null +++ b/lib/plum/rack/cli.rb @@ -0,0 +1,125 @@ +require "optparse" +require "rack/builder" + +module Plum + module Rack + class CLI + def initialize(argv) + @argv = argv + @options = {} + + parse! + end + + def run + @server.start + end + + private + def parse! + @parser = setup_parser + @parser.parse!(@argv) + + config = transform_options + # TODO: parse rack_opts? + rack_app, rack_opts = ::Rack::Builder.parse_file(@argv.shift || "config.ru") + + @server = Plum::Rack::Server.new(rack_app, config) + end + + def transform_options + if @options[:config] + dsl = DSL::Config.new.instance_eval(File.read(@options[:config])) + config = dsl.config + else + config = Config.new + end + + ENV["RACK_ENV"] = @options[:env] if @options[:env] + config[:debug] = @options[:debug] unless @options[:debug].nil? + config[:server_push] = @options[:server_push] unless @options[:server_push].nil? + + if @options[:socket] + config[:listeners] << { listener: UNIXListener, + path: @options[:socket] } + end + + if !@options[:socket] || @options[:host] || @options[:port] + if @options[:tls] == false + config[:listeners] << { listener: TCPListener, + hostname: @options[:host] || "0.0.0.0", + port: @options[:port] || 8080 } + else + config[:listeners] << { listener: TLSListener, + hostname: @options[:host] || "0.0.0.0", + port: @options[:port] || 8080, + certificate: @options[:cert] && File.read(@options[:cert]), + certificate_key: @options[:cert] && File.read(@options[:key]) } + end + end + + config + end + + def setup_parser + parser = OptionParser.new do |o| + o.on "-C", "--config PATH", "Load PATH as a config" do |arg| + @options[:config] = arg + end + + o.on "-D", "--debug", "Run puma in debug mode" do + @options[:debug] = true + end + + o.on "-e", "--environment ENV", "Rack environment (default: development)" do |arg| + @options[:env] = arg + end + + o.on "-a", "--address HOST", "Bind to host HOST (default: 0.0.0.0)" do |arg| + @options[:host] = arg + end + + o.on "-p", "--port PORT", "Bind to port PORT (default: 8080)" do |arg| + @options[:port] = arg.to_i + end + + o.on "-S", "--socket PATH", "Bind to UNIX domain socket" do |arg| + @options[:socket] = arg + end + + o.on "--http", "Use http URI scheme (use raw TCP)" do |arg| + @options[:tls] = false + end + + o.on "--https", "Use https URI scheme (use TLS; default)" do |arg| + @options[:tls] = true + end + + o.on "--server-push BOOL", "Enable HTTP/2 server push" do |arg| + @options[:server_push] = arg != "false" + end + + o.on "--cert PATH", "Use PATH as server certificate" do |arg| + @options[:cert] = arg + end + + o.on "--key PATH", "Use PATH as server certificate's private key" do |arg| + @options[:key] = arg + end + + o.on "-v", "--version", "Show version" do + puts "plum version #{::Plum::VERSION}" + exit(0) + end + + o.on "-h", "--help", "Show this message" do + puts o + exit(0) + end + + o.banner = "plum [options] [rackup config file]" + end + end + end + end +end diff --git a/lib/plum/rack/config.rb b/lib/plum/rack/config.rb index 2f04886..0ab04c5 100644 --- a/lib/plum/rack/config.rb +++ b/lib/plum/rack/config.rb @@ -8,7 +8,7 @@ module Plum server_push: true }.freeze - def initialize(config) + def initialize(config = {}) @config = DEFAULT_CONFIG.merge(config) end @@ -16,6 +16,10 @@ module Plum @config[key] end + def []=(key, value) + @config[key] = value + end + def to_s @config.to_s end diff --git a/lib/plum/rack/listener.rb b/lib/plum/rack/listener.rb index 83a3dff..02e00a1 100644 --- a/lib/plum/rack/listener.rb +++ b/lib/plum/rack/listener.rb @@ -88,9 +88,26 @@ module Plum end class UNIXListener < BaseListener - def initialize(path, permission, user, group) - @server = ::UNIXServer.new(path) - # TODO: set permission, user, group + def initialize(lc) + if File.exist?(lc[:path]) + begin + old = UNIXSocket.new(lc[:path]) + rescue SystemCallError, IOError + File.unlink(lc[:path]) + else + old.close + raise "Already a server bound to: #{lc[:path]}" + end + end + + @server = ::UNIXServer.new(lc[:path]) + + File.chmod(lc[:mode], lc[:path]) if lc[:mode] + end + + def stop + super + File.unlink(lc[:path]) end def to_io -- cgit v1.2.3