Class: HTTPkit::Server

Inherits:
Object
  • Object
show all
Includes:
Connection::Status, HTTPkit::Support::HandlerManager::Setup
Defined in:
lib/httpkit/server.rb

Defined Under Namespace

Classes: BlockHandler, BodyHandler, KeepAliveHandler, MandatoryHandler, TimeoutsHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection::Status

#close, #closed?, #error?, #network_fault?, #timeout?

Methods included from HTTPkit::Support::HandlerManager::Setup

#setup_handlers

Constructor Details

#initialize(config, connection) ⇒ Server

Returns a new instance of Server



14
15
16
17
18
19
20
21
# File 'lib/httpkit/server.rb', line 14

def initialize(config, connection)
  @sequence = 0
  @config = config
  (@config[:handlers] ||= []).push(MandatoryHandler.new, BodyHandler.new)

  setup_connection(connection)
  setup_handlers
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config



12
13
14
# File 'lib/httpkit/server.rb', line 12

def config
  @config
end

Class Method Details

.start(config) ⇒ Object



5
6
7
# File 'lib/httpkit/server.rb', line 5

def self.start(config)
  Connection::EventMachine.start_server(config, self)
end

Instance Method Details

#finish(request) ⇒ Object



39
40
41
# File 'lib/httpkit/server.rb', line 39

def finish(request)
  @handlers.invoke(:finish, request)
end

#respond(request, response) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/httpkit/server.rb', line 30

def respond(request, response)
  fiber = Fiber.new do
    request, response = @handlers.invoke(:respond, request, response)
    @connection.serialize(response)
    finish(request)
  end
  fiber.resume
end

#serve(request) ⇒ Object



23
24
25
26
27
28
# File 'lib/httpkit/server.rb', line 23

def serve(request)
  served = Promise.new
  served.then { |response| respond(request, response) }

  serve!(request, served).resume
end