Class: Rosendo::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rosendo/server.rb

Defined Under Namespace

Classes: Stop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
# File 'lib/rosendo/server.rb', line 8

def initialize(app, args = {})
  @app = app
  @port = args[:port] || '2000'
  @out = args[:out] || STDOUT
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



7
8
9
# File 'lib/rosendo/server.rb', line 7

def out
  @out
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/rosendo/server.rb', line 7

def port
  @port
end

Instance Method Details

#serverObject



33
34
35
# File 'lib/rosendo/server.rb', line 33

def server
  @server ||= TCPServer.new(@port)
end

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rosendo/server.rb', line 14

def start
  out.puts "== Rosendo is rocking the stage on #{port}"
  out.puts ">> Listening on 0.0.0.0:#{port}, CTRL+C to stop"
  
  loop do
    begin
      client = server.accept
      request = Request.new(client)
      response = Response.new(client)
      @app.process(request, response)
      response.respond
      out.puts "#{request.method} #{request.url} #{response.status} #{response.body.size}"
    rescue Stop
      server.close
      out.puts "== Rosendo has left the building (everybody goes crazy)"
    end
  end
end