Class: Goliath::RackProxy

Inherits:
API
  • Object
show all
Defined in:
lib/goliath/rack_proxy.rb

Defined Under Namespace

Classes: RackCall, RackInput

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rack_app(app) ⇒ Object

Rack app to proxy the incoming requests to.



10
11
12
# File 'lib/goliath/rack_proxy.rb', line 10

def self.rack_app(app)
  rack_proxy_options[:rack_app] = app
end

.rack_proxy_optionsObject

Custom user-defined options.



20
21
22
# File 'lib/goliath/rack_proxy.rb', line 20

def self.rack_proxy_options
  @rack_proxy_options ||= {}
end

.rewindable_input(value) ⇒ Object

Whether the request body should be rewindable.



15
16
17
# File 'lib/goliath/rack_proxy.rb', line 15

def self.rewindable_input(value)
  rack_proxy_options[:rewindable_input] = value
end

Instance Method Details

#on_body(env, data) ⇒ Object

Resumes the Rack request with the received request body data.



34
35
36
# File 'lib/goliath/rack_proxy.rb', line 34

def on_body(env, data)
  env["rack_proxy.call"].resume(data, on_response: -> (response) { send_response(response, env) })
end

#on_close(env) ⇒ Object

Resumes the Rack request with no more data.



39
40
41
# File 'lib/goliath/rack_proxy.rb', line 39

def on_close(env)
  env["rack_proxy.call"].resume
end

#on_headers(env, headers) ⇒ Object

Starts the request to the given Rack application.



25
26
27
28
29
30
31
# File 'lib/goliath/rack_proxy.rb', line 25

def on_headers(env, headers)
  rack_app         = self.class.rack_proxy_options.fetch(:rack_app)
  rewindable_input = self.class.rack_proxy_options.fetch(:rewindable_input, true)

  env["rack_proxy.call"] = RackCall.new(rack_app, env, rewindable_input: rewindable_input)
  env["rack_proxy.call"].resume(on_response: -> (response) { send_response(response, env) })
end

#response(env) ⇒ Object

Resumes the Rack request with no more data.



44
45
46
47
# File 'lib/goliath/rack_proxy.rb', line 44

def response(env)
  env["rack_proxy.call"].resume(on_response: -> (response) { send_response(response, env) })
  nil
end