Class: Reifier::Server

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Server.



3
4
5
6
# File 'lib/reifier/server.rb', line 3

def initialize(app, options = {})
  @app     = app
  @options = options
end

Instance Method Details

#startObject



8
9
10
11
12
13
14
15
16
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
46
47
48
49
50
51
52
53
# File 'lib/reifier/server.rb', line 8

def start
  Thread.abort_on_exception = true
  Signal.trap 'SIGINT' do
    puts "\nCleaning up nothing for now..."
    exit
  end

  server = TCPServer.new(@options[:Host], @options[:Port])
  pool   = Concurrent::FixedThreadPool.new(5)

  puts "# Environment: #{@options[:environment]}"
  puts "# Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
  puts "# PID: #{Process.pid}"

  loop do
    socket = server.accept
    socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

    pool.post do
      begin
        request  = Request.new(@options)
        response = Response.new

        request.handle(socket)

        response.request_method = request.request_method
        response.request_uri    = request.request_uri
        response.protocol       = request.protocol

        response << @app.call(request.rack_env)

        response.handle(socket)

        log(request, response) if @options[:environment] == 'development'
      rescue EOFError
        puts 'Request Line is empty'
      rescue HTTPParseError
        puts 'Request Line must include HTTP (HTTPS enabled?)'
      rescue Errno::EPIPE
        puts 'Writing to client failed :|'
      ensure
        socket.close
      end
    end
  end
end