Class: Jets::Controller::Handler::Apigw
- Inherits:
-
Object
- Object
- Jets::Controller::Handler::Apigw
- Extended by:
- Memoist
- Defined in:
- lib/jets/controller/handler/apigw.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#meth ⇒ Object
readonly
Returns the value of attribute meth.
-
#rack_env ⇒ Object
readonly
Returns the value of attribute rack_env.
Instance Method Summary collapse
- #add_response_headers(resp, headers) ⇒ Object
-
#adjust_for_elb(resp) ⇒ Object
Note: ELB is not officially support.
-
#convert_to_api_gateway(status, headers, body) ⇒ Object
Transform the structure to AWS_PROXY compatible structure docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format.
- #from_elb? ⇒ Boolean
-
#initialize(event, context, controller, meth, rack_env) ⇒ Apigw
constructor
A new instance of Apigw.
-
#process_through_middlewares ⇒ Object
1.
Constructor Details
#initialize(event, context, controller, meth, rack_env) ⇒ Apigw
Returns a new instance of Apigw.
10 11 12 13 14 15 16 |
# File 'lib/jets/controller/handler/apigw.rb', line 10 def initialize(event, context, controller, meth, rack_env) @event = event @context = context @controller = controller @meth = meth @rack_env = rack_env end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/jets/controller/handler/apigw.rb', line 9 def context @context end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
9 10 11 |
# File 'lib/jets/controller/handler/apigw.rb', line 9 def controller @controller end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
9 10 11 |
# File 'lib/jets/controller/handler/apigw.rb', line 9 def event @event end |
#meth ⇒ Object (readonly)
Returns the value of attribute meth.
9 10 11 |
# File 'lib/jets/controller/handler/apigw.rb', line 9 def meth @meth end |
#rack_env ⇒ Object (readonly)
Returns the value of attribute rack_env.
9 10 11 |
# File 'lib/jets/controller/handler/apigw.rb', line 9 def rack_env @rack_env end |
Instance Method Details
#add_response_headers(resp, headers) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/jets/controller/handler/apigw.rb', line 64 def add_response_headers(resp, headers) resp['headers'] = headers.reject { |_, val| val.is_a?(Array) } multi_value_headers = headers.select { |_, val| val.is_a?(Array) } resp['multiValueHeaders'] = multi_value_headers unless multi_value_headers.blank? end |
#adjust_for_elb(resp) ⇒ Object
Note: ELB is not officially support. This is just in case users wish to manually connect ELBs to the functions created by Jets.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jets/controller/handler/apigw.rb', line 73 def adjust_for_elb(resp) return resp unless from_elb? # ELB requires statusCode to be an Integer whereas API Gateway requires statusCode to be a String status = resp["statusCode"] = resp["statusCode"].to_i # ELB also requires statusDescription attribute status_desc = Rack::Utils::HTTP_STATUS_CODES[status] status_desc = status_desc.nil? ? status.to_s : "#{status} #{status_desc}" resp["statusDescription"] = status_desc resp end |
#convert_to_api_gateway(status, headers, body) ⇒ Object
Transform the structure to AWS_PROXY compatible structure docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jets/controller/handler/apigw.rb', line 50 def convert_to_api_gateway(status, headers, body) base64 = headers["x-jets-base64"] == 'yes' body = body.respond_to?(:read) ? body.read : body body = Base64.encode64(body) if base64 {}.tap do |resp| resp['statusCode'] = status resp['body'] = body resp['isBase64Encoded'] = base64 add_response_headers(resp, headers) adjust_for_elb(resp) end end |
#from_elb? ⇒ Boolean
87 88 89 90 91 |
# File 'lib/jets/controller/handler/apigw.rb', line 87 def from_elb? # NOTE: @event["requestContext"]["elb"] is set when the request is coming from an elb # Can set JETS_ELB=1 for local testing @event["requestContext"] && @event["requestContext"]["elb"] || ENV['JETS_ELB'] end |
#process_through_middlewares ⇒ Object
-
Convert API Gateway event event to Rack env
-
Process using full Rack middleware stack
-
Convert back to API gateway response structure payload
Returns back API Gateway response hash structure Only called when running in AWS Lambda.
Set the jets.* env variables so we have access to them in the middleware. The controller instance is called in the Main middleware. On AWS, will use original event and context. On local server, will use Middleware::Mimic event and context.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jets/controller/handler/apigw.rb', line 29 def process_through_middlewares env = rack_env.merge( 'jets.controller' => @controller, # original controller instance from handler 'jets.context' => @context, # original AWS Lambda context 'jets.event' => @event, # original AWS Lambda event 'jets.meth' => @meth, ) status, headers, body = Jets.application.call(env) # goes through full middleware stack # middleware can handle Array or BodyProxy, APIGW require Strings case body when Rack::Files::Iterator str = File.read(body.path) else # join for Rack::BodyProxy or Array str = body.join end convert_to_api_gateway(status, headers, str) end |