Class: Berkshelf::API::RESTGateway
- Inherits:
-
Reel::Server::HTTP
- Object
- Reel::Server::HTTP
- Berkshelf::API::RESTGateway
- 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
- #app ⇒ Berkshelf::API::RackApp readonly
- #host ⇒ String readonly
- #port ⇒ Integer readonly
- #workers ⇒ Integer readonly
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RESTGateway
constructor
A new instance of RESTGateway.
- #on_connect(connection) ⇒ Object
- #request_env(request, connection) ⇒ Object
- #route_request(connection, request) ⇒ Object
Methods included from Logging
Methods included from GenericServer
Constructor Details
#initialize(options = {}) ⇒ RESTGateway
Returns a new instance of RESTGateway.
83 84 85 86 87 88 89 90 91 |
# File 'lib/berkshelf/api/rest_gateway.rb', line 83 def initialize( = {}) = DEFAULT_OPTIONS.merge() @host = [:host] @port = [:port] log.info "REST Gateway listening on #{@host}:#{@port}" super(@host, @port, &method(:on_connect)) @app = Berkshelf::API::RackApp.new end |
Instance Attribute Details
#app ⇒ Berkshelf::API::RackApp (readonly)
75 76 77 |
# File 'lib/berkshelf/api/rest_gateway.rb', line 75 def app @app end |
#host ⇒ String (readonly)
66 67 68 |
# File 'lib/berkshelf/api/rest_gateway.rb', line 66 def host @host end |
#port ⇒ Integer (readonly)
69 70 71 |
# File 'lib/berkshelf/api/rest_gateway.rb', line 69 def port @port end |
#workers ⇒ Integer (readonly)
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 |