Class: Berkshelf::API::RESTGateway

Inherits:
Reel::Server::HTTP
  • Object
show all
Extended by:
Forwardable
Includes:
GenericServer, Logging
Defined in:
lib/berkshelf/api/rest_gateway.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  host: '0.0.0.0',
  port: 26200,
  quiet: false,
  workers: 10
}.freeze
INITIAL_BODY =
''
CONTENT_TYPE_ORIG =
'Content-Type'.freeze
CONTENT_LENGTH_ORIG =
'Content-Length'.freeze
CONTENT_TYPE =
'CONTENT_TYPE'.freeze
CONTENT_LENGTH =
'CONTENT_LENGTH'.freeze
SERVER_SOFTWARE =
'SERVER_SOFTWARE'.freeze
SERVER_NAME =
'SERVER_NAME'.freeze
SERVER_PORT =
'SERVER_PORT'.freeze
SERVER_PROTOCOL =
'SERVER_PROTOCOL'.freeze
GATEWAY_INTERFACE =
"GATEWAY_INTERFACE".freeze
LOCALHOST =
'localhost'.freeze
HTTP_VERSION =
'HTTP_VERSION'.freeze
CGI_1_1 =
'CGI/1.1'.freeze
REMOTE_ADDR =
'REMOTE_ADDR'.freeze
CONNECTION =
'HTTP_CONNECTION'.freeze
SCRIPT_NAME =
'SCRIPT_NAME'.freeze
PATH_INFO =
'PATH_INFO'.freeze
REQUEST_METHOD =
'REQUEST_METHOD'.freeze
QUERY_STRING =
'QUERY_STRING'.freeze
HTTP_1_0 =
'HTTP/1.0'.freeze
HTTP_1_1 =
'HTTP/1.1'.freeze
HTTP_ =
'HTTP_'.freeze
HOST =
'Host'.freeze
RACK_INPUT =
'rack.input'.freeze
RACK_LOGGER =
'rack.logger'.freeze
RACK_VERSION =
'rack.version'.freeze
RACK_ERRORS =
'rack.errors'.freeze
RACK_MULTITHREAD =
'rack.multithread'.freeze
RACK_MULTIPROCESS =
'rack.multiprocess'.freeze
RACK_RUN_ONCE =
'rack.run_once'.freeze
RACK_URL_SCHEME =
'rack.url_scheme'.freeze
RACK_WEBSOCKET =
'rack.websocket'.freeze
PROTO_RACK_ENV =
{
  RACK_VERSION      => ::Rack::VERSION,
  RACK_ERRORS       => STDERR,
  RACK_MULTITHREAD  => true,
  RACK_MULTIPROCESS => false,
  RACK_RUN_ONCE     => false,
  RACK_URL_SCHEME   => "http".freeze,
  SCRIPT_NAME       => ENV[SCRIPT_NAME] || "",
  SERVER_PROTOCOL   => HTTP_1_1,
  SERVER_SOFTWARE   => "berkshelf-api/#{Berkshelf::API::VERSION}".freeze,
  GATEWAY_INTERFACE => CGI_1_1
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

init, #logger

Methods included from GenericServer

included

Constructor Details

#initialize(options = {}) ⇒ RESTGateway

Returns a new instance of RESTGateway.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :host (String) — default: '0.0.0.0'
  • :port (Integer) — default: 26200
  • :quiet (Boolean) — default: false
  • :workers (Integer) — default: 10


83
84
85
86
87
88
89
90
91
# File 'lib/berkshelf/api/rest_gateway.rb', line 83

def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  @host   = options[:host]
  @port   = options[:port]

  log.info "REST Gateway listening on #{@host}:#{@port}"
  super(@host, @port, &method(:on_connect))
  @app = Berkshelf::API::RackApp.new
end

Instance Attribute Details

#appBerkshelf::API::RackApp (readonly)



75
76
77
# File 'lib/berkshelf/api/rest_gateway.rb', line 75

def app
  @app
end

#hostString (readonly)

Returns:

  • (String)


66
67
68
# File 'lib/berkshelf/api/rest_gateway.rb', line 66

def host
  @host
end

#portInteger (readonly)

Returns:

  • (Integer)


69
70
71
# File 'lib/berkshelf/api/rest_gateway.rb', line 69

def port
  @port
end

#workersInteger (readonly)

Returns:

  • (Integer)


72
73
74
# File 'lib/berkshelf/api/rest_gateway.rb', line 72

def workers
  @workers
end

Instance Method Details

#on_connect(connection) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/berkshelf/api/rest_gateway.rb', line 93

def on_connect(connection)
  while request = connection.request
    case request
    when request.websocket?
      request.respond(:bad_request, "WebSockets not supported")
    else
      route_request(connection, request)
    end
  end
end

#request_env(request, connection) ⇒ Object



113
114
115
116
117
# File 'lib/berkshelf/api/rest_gateway.rb', line 113

def request_env(request, connection)
  env = env(request)
  env[REMOTE_ADDR] = connection.remote_ip
  env
end

#route_request(connection, request) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/berkshelf/api/rest_gateway.rb', line 104

def route_request(connection, request)
  status, headers, body_parts = app.call(request_env(request, connection))
  body, is_stream             = response_body(body_parts)

  response_klass = is_stream ? Reel::StreamResponse : Reel::Response
  response       = response_klass.new(status, headers, body)
  connection.respond(response)
end