Class: Fishwife::HttpServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpServer

Create the server with specified options:

:host

String specifying the IP address to bind to (default: 0.0.0.0)

:port

String or integer with the port to bind to (default: 9292). Jetty picks if given port 0 (and port can be read on return from start.)

: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)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fishwife/http_server.rb', line 43

def initialize( options = {} )
  super()

  @server = nil
  @host = 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].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'

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

Instance Attribute Details

#hostObject

Returns the value of attribute host.



20
21
22
# File 'lib/fishwife/http_server.rb', line 20

def host
  @host
end

Instance Method Details

#create_connectors(*args) ⇒ Object



93
94
95
96
97
# File 'lib/fishwife/http_server.rb', line 93

def create_connectors( *args )
  super.tap do |ctrs|
    ctrs.first.host = @host if ctrs.first
  end
end

#joinObject

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



84
85
86
# File 'lib/fishwife/http_server.rb', line 84

def join
  @server.join if @server
end

#start(app) ⇒ Object

Start the server, given rack app to run



72
73
74
75
76
77
78
79
80
81
# File 'lib/fishwife/http_server.rb', line 72

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

  @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



89
90
91
# File 'lib/fishwife/http_server.rb', line 89

def stop
  @server.stop if @server
end