Class: Kwipper::HttpServer

Inherits:
TCPServer
  • Object
show all
Defined in:
lib/kwipper/http_server.rb

Constant Summary collapse

DEFAULT_PORT =
80

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bind = '127.0.0.1', port = 7335) ⇒ HttpServer

Returns a new instance of HttpServer.



10
11
12
13
14
15
# File 'lib/kwipper/http_server.rb', line 10

def initialize(bind = '127.0.0.1', port = 7335)
  @bind, @port = bind, port
  @host = "#@bind#{":#@port" unless port.to_i == DEFAULT_PORT}"
  log.debug "Starting server on #@host"
  super
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/kwipper/http_server.rb', line 4

def host
  @host
end

Class Method Details

.run(bind = '127.0.0.1', port = 7335) ⇒ Object



6
7
8
# File 'lib/kwipper/http_server.rb', line 6

def self.run(bind = '127.0.0.1', port = 7335)
  HttpServer.new(bind, port).serve
end

Instance Method Details

#serveObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kwipper/http_server.rb', line 17

def serve
  load_models
  load_controllers
  parser = HttpParser.new
  Kwipper.log_startup_time

  while socket = accept
    begin
      request = parser.parse socket

      log.info "#{request.info} #{request.params.inspect unless request.params.empty?}".strip.green

      response = Application.new.respond_to request
      socket.write response.to_http_response

    rescue Errno::ECONNRESET, Errno::EPIPE => e
      log.info "#{e.class} #{e.message}".yellow
    rescue Kwipper::EmptyRequest => e
      log.warn "#{e.class} #{e.message}".yellow
    ensure
      socket.close
    end
  end

rescue Interrupt
  socket.close if socket && !socket.closed?
  Model.db.close
  log.debug "Ok bye."
end