Class: PHPRPC::WEBrickServer

Inherits:
WEBrickServlet show all
Defined in:
lib/phprpc/webrick_server.rb

Overview

class WEBrickServlet

Instance Attribute Summary

Attributes inherited from BaseServer

#charset, #debug

Instance Method Summary collapse

Methods inherited from WEBrickServlet

#get_instance, #service

Methods inherited from BaseServer

#add, #call, #call!

Constructor Details

#initialize(options = {}) ⇒ WEBrickServer

Returns a new instance of WEBrickServer.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/phprpc/webrick_server.rb', line 62

def initialize(options = {})
  super()
  @app = self
  @options = {
    :BindAddress    => '0.0.0.0',
    :Port           => 3000,
    :Logger         => nil,
    :MaxClients     => 100,
    :RequestTimeout => 30,
    :session_mode   => :file,
    :path           => "/",
    :expire_after   => 1800,
  }.update(options)
  @opts = OptionParser.new
  @opts.banner = "Usage: #{@opts.program_name} webrick [options]"
  @opts.separator ""
  @opts.separator "Server options:"
  @opts.on('-a', '--address IP', String, "Address to bind to (default: #{@options[:BindAddress]})") { |ip| @options[:BindAddress] = ip }
  @opts.on('-p', '--port PORT', Integer, "Which port to bind to (default: #{@options[:Port]})") { |port| @options[:Port] = port }
  @opts.on('-l', '--log FILE', String, "File to redirect output") { |file| @options[:Logger] = WEBrick::Log.new(file); }
  @opts.on('-L', '--accesslog FILE', String, "File to redirect access info") { |file|
    file = open(file, "a+")
    @options[:AccessLog] = [
      [ file, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
      [ file, WEBrick::AccessLog::REFERER_LOG_FORMAT ]
    ]
  }
  @opts.separator ""
  @opts.separator "Tuning options:"
  @opts.on('-n', '--max-clients INT', Integer, "Maximum number of the concurrent", "connections (default: #{@options[:MaxClients]})") { |num| @options[:MaxClients] = num }
  @opts.on('-t', '--timeout TIME', Integer, "Request timeout (in seconds)", "(default: #{@options[:RequestTimeout]})") { |time| @options[:RequestTimeout] = time }
  @opts.separator ""
  @opts.separator "Session options:"
  @opts.on('-s', '--session-mode MODE', [:file, :memcache, :pool], "Select Session mode (file, memcache, pool)", "(default: #{@options[:session_mode].to_s})") { |mode| @options[:session_mode] = mode }
  @opts.on('-e','--expire-after TIME', Integer, "Session expire after (default: #{@options[:expire_after]})") { |time| @options[:expire_after] = time }
  @opts.on('-m','--memcache_server SERVER', String, "Memcache server used by session in", "memcache mode (default: localhost:11211)") { |server| @options[:memcache_server] = server }
  @opts.on('--prefix PATH', String, "Mount the PHPRPC Server under PATH", "(start with /)") { |path| @options[:path] = path }
  @opts.separator ""
  @opts.separator "Common options:"
  @opts.on_tail('-D', '--debug', "Set debbuging on (default: disabled)") { self.debug = true }
  @opts.on_tail('-?', '-h', '--help', "Show this help message.") { puts @opts; exit }
  @opts.on_tail('-v', '--version', "Show version") { puts "WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})"; exit }
  begin
    @opts.parse!(ARGV)
  rescue OptionParser::ParseError
    puts @opts
    exit
  end
end

Instance Method Details

#startObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/phprpc/webrick_server.rb', line 112

def start
  if [:memcache, :pool].include?(@options[:session_mode]) then
    old_app = @app
    begin
      require 'rubygems'
      require 'rack'
      if @options[:session_mode] == :memcache then
        @app = Rack::Session::Memcache.new(@app, @options)
      else
        @app = Rack::Session::Pool.new(@app, @options)
      end
    rescue Exception
      @app = old_app
    end
  end
  @options.delete(:ServerType)
  @options.delete(:DocumentRoot)
  @options.delete(:RequestCallback)
  @options.delete(:RequestHandler)
  server = WEBrick::HTTPServer.new(@options)
  server.mount(@options[:path], self)
  trap(:INT) { server.shutdown }
  server.start
end