Class: Rack::Handler::Webbit

Inherits:
Object
  • Object
show all
Includes:
HttpHandler
Defined in:
lib/rack/handler/webbit.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Webbit

Returns a new instance of Webbit.



36
37
38
# File 'lib/rack/handler/webbit.rb', line 36

def initialize(app)
  @app = app
end

Class Method Details

.run(app, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack/handler/webbit.rb', line 15

def self.run(app, options={})
  port = (options[:port] || options[:Port]).to_i
  worker_threads = options.fetch(:worker_threads, 1).to_i
  @executor = Executors.new_fixed_thread_pool(worker_threads)
  @handler = Webbit.new(app)
  @server = WebServers.create_web_server(@executor, port)
  @server.add(@handler)
  @server.start
  $stderr.puts("Webbit server started on port #{port}, #{worker_threads} worker threads")
  install_shutdown_hook!
end

.shutdownObject



27
28
29
30
31
32
33
34
# File 'lib/rack/handler/webbit.rb', line 27

def self.shutdown
  @server.stop
  $stderr.puts('Webbit server shutting down')
  @server.join
  @executor.shutdown
  @executor, @server, @handler = nil, nil, nil
  $stderr.puts('Webbit server stopped')
end

Instance Method Details

#handleHttpRequest(request, response, control) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack/handler/webbit.rb', line 40

def handleHttpRequest(request, response, control)
  # TODO: there is something not working with chunked responses
  status, headers, body = @app.call(create_env(request))
  headers.each do |key, value|
    response.header(key, value)
  end
  body.each do |chunk|
    response.content(chunk.to_java_bytes)
  end
  response.status(status)
  body.close if body.respond_to?(:close)
rescue => e
  response.content(e.message + "\n\n")
  response.content(e.backtrace.join("\n"))
  response.status(500)
ensure
  response.end
end