Class: Fishwife::HttpServer

Inherits:
RJack::Jetty::ServerFactory
  • Object
show all
Defined in:
lib/fishwife/http_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpServer

Create the server with specified options:

:host

The interface to bind to (default: 0.0.0.0 -> all)

:port

The local port to bind to, for the first connection. Jetty picks if given port 0, and first connection port can be read on return from start. (default: 9292)

:connections

An array, or a string that will be split on ‘|’, where each element is a connection URI String or a hash of parameters. See details below.

:min_threads

Minimum number of threads to keep in pool (default: 5)

:max_threads

Maximum threads to create in pool (default: 50)

:max_idle_time_ms

Maximum idle time for a connection in milliseconds (default: 10_000)

:request_log_file

Request log to file name or :stderr (default: nil, no log)

:request_body_ram

Maximum size of request body (i.e POST) to keep in memory before resorting to a temporary file (default: 256 KiB)

:request_body_tmpdir

Path to where request body temporary files should be created (when request_body_ram is exceeded.) (default: Dir.tmpdir)

:request_body_max

Maximum total size of a request body, after which the request will be rejected with status 413. This limit is provided to avoid pathologic resource exhaustion. (default: 8 MiB)

Options in connections

Each member of the connections array is either a hash with the following properties or an equivalent URI string:

:scheme

Values ‘tcp’ or ‘ssl’

:host

The local interface to bind (default: top level #host or 0.0.0.0)

:port

Port number or 0 to select an available port (default: top level #port for first connection or 0)

:max_idle_time_ms

See above

:key_store_path

For ssl, the path to the (Java JKS) keystore

:key_store_password

For ssl, the password to the keystore

URI examples:

tcp://127.0.0.1
ssl://0.0.0.0:8443?key_store_path=keystore&key_store_password=399as8d9


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
111
112
113
114
# File 'lib/fishwife/http_server.rb', line 79

def initialize( options = {} )
  super()

  @server = nil

  self.min_threads = 5
  self.max_threads = 50
  self.port = 9292

  options = Hash[ options.map { |o| [ o[0].to_s.downcase.to_sym, o[1] ] } ]

  # Translate option values from possible Strings
  [ :port, :min_threads, :max_threads, :max_idle_time_ms,
    :request_body_ram, :request_body_max ].each do |k|
    v = options[k]
    options[k] = v.to_i if v
  end

  v = options[ :request_log_file ]
  options[ :request_log_file ] = v.to_sym if v == 'stderr'

  v = options[ :connections ]
  options[ :connections ] = v.split('|') if v.is_a?( String )

  # Split out servlet options.
  @servlet_options = {}
  [ :request_body_ram, :request_body_tmpdir, :request_body_max ].each do |k|
    @servlet_options[k] = options.delete(k)
  end

  # Apply remaining options as setters
  options.each do |k,v|
    setter = "#{k}=".to_sym
    send( setter, v ) if respond_to?( setter )
  end
end

Instance Method Details

#joinObject

Join with started server so main thread doesn’t exit.



130
131
132
# File 'lib/fishwife/http_server.rb', line 130

def join
  @server.join if @server
end

#start(app) ⇒ Object

Start the server, given rack app to run



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fishwife/http_server.rb', line 117

def start( app )
  set_context_servlets( '/',
                        {'/*' => RackServlet.new(app, @servlet_options)} )

  @server = create
  @server.start
  # Recover the server port in case 0 was given.
  self.port = @server.connectors[0].local_port

  @server
end

#stopObject

Stop the server to allow graceful shutdown



135
136
137
# File 'lib/fishwife/http_server.rb', line 135

def stop
  @server.stop if @server
end