Class: Wildsonet::Server::Handler

Inherits:
Object
  • Object
show all
Includes:
RackProxy
Defined in:
lib/wildsonet-server.rb

Overview

Rack handler utilizing Netty library. Works with nginx as frontend server to proxy the requests. Netty handles only dynamic requests. Static requests are handled by nginx proxy.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Handler

Setup the server.

Parameters:

  • app

    Rack application to start

  • options

    Options for server



56
57
58
59
# File 'lib/wildsonet-server.rb', line 56

def initialize(app, options)
  @app     = ::Rack::Lint.new(app)
  @options = options
end

Class Method Details

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

Starts the server.

Parameters:

  • app

    Rack application to start

  • options (defaults to: {})

    Options for server



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wildsonet-server.rb', line 36

def self.run app, options = {}
  # Set default options
  options[:Port]      ||= 3000
  options[:Host]      ||= "0.0.0.0"

  # Create new server
  @@server            = Server.new(options[:Host], options[:Port], self.new(app, options))

  Thread.new do
    while true
      Kernel.sleep 10
    end
  end.join

end

Instance Method Details

#call(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wildsonet-server.rb', line 61

def call env

  ruby = {}

  env.keySet.each do |key|
    case key
      when "rack.input"
        ruby[key] = env[key].to_io
      when "rack.errors"
        ruby[key] = env[key].to_io
      else
        ruby[key] = env[key]
    end
  end

  ruby["rack.version"] = ::Rack::VERSION

  status, headers, body = @app.call(ruby)

  unless headers.include?("X-Handled")

    response = DefaultHttpResponse.new(HttpVersion::HTTP_1_1, HttpResponseStatus.valueOf(status))

    headers.each_pair do |header, value|
      response.addHeader(header, value)
    end

    buffer = ChannelBuffers.dynamicBuffer

    body.each do |line|
      buffer.writeBytes(line.to_s.to_java_bytes)
    end

    response.content = buffer

    future = env["wsn.context"].channel.write(response)

    future.addListener(ChannelFutureListener::CLOSE)
    
  end

  env["rack.input"].close
  File.delete(env["wsn.tempfile"])
  
end