Class: Jets::Controller::Middleware::Local

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/controller/middleware/local/api_gateway.rb,
lib/jets/controller/middleware/local.rb,
lib/jets/controller/middleware/local/route_matcher.rb,
lib/jets/controller/middleware/local/mimic_aws_call.rb

Overview

Takes a Rack env and converts to ApiGateway event

Defined Under Namespace

Classes: ApiGateway, MimicAwsCall, RouteMatcher

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Local

Returns a new instance of Local.



8
9
10
# File 'lib/jets/controller/middleware/local.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jets/controller/middleware/local.rb', line 12

def call(env)
  route = RouteMatcher.new(env).find_route
  unless route
    return [404, {'Content-Type' => 'text/html'}, not_found(env)]
  end

  mimic = MimicAwsCall.new(route, env)
  # Make @controller and @meth instance available so we dont not have to pass it around.
  @controller, @meth, @event = mimic.controller, mimic.meth, mimic.event

  if route.to == 'jets/rack#process' # megamode
    # Bypass the Jets middlewares since it could interfere with the Rack
    # application's middleware stack.
    #
    # Rails sends back a transfer-encoding=chunked. Curling Rails directly works,
    # but passing the Rails response back through this middleware results in errors.
    # Disable chunking responses by deleting the transfer-encoding response header.
    # Would like to understand why this happens this better, if someone can explain please let me know.
    status, headers, body = @controller.dispatch! # jets/rack_controller
    headers.delete "transfer-encoding"
    [status, headers, body]
  elsif route.to == 'jets/mount#call' # mount route
    status, headers, body = @controller.dispatch! # jets/mount_controller
    [status, headers, body]
  elsif polymorphic_function?
    run_polymophic_function
  else # Normal Jets request
    mimic_aws_lambda!(env, mimic.vars) unless on_aws?(env)
    @app.call(env)
  end
end

#mimic_aws_lambda!(env, vars) ⇒ Object

Modifies env the in the same way real call from AWS lambda would modify env



66
67
68
69
# File 'lib/jets/controller/middleware/local.rb', line 66

def mimic_aws_lambda!(env, vars)
  env.merge!(vars)
  env
end

#not_found(env) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jets/controller/middleware/local.rb', line 87

def not_found(env)
  message = routes_error_message(env)
  body = <<~HTML
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Route not found</title>
        </head>
        <body>
          #{message}
        </body>
    </html>
  HTML
  StringIO.new(body)
end

#on_aws?(env) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/jets/controller/middleware/local.rb', line 71

def on_aws?(env)
  return false if Jets.env.test? # usually with test we're passing in full API Gateway fixtures with the HTTP_X_AMZN_TRACE_ID
  return false if ENV['JETS_ELB'] # If we're using an ELB and Jets is inside a container running jets server, we don't want to pretend we're on AWS.
  on_cloud9 = !!(env['HTTP_HOST'] =~ /cloud9\..*\.amazonaws\.com/)
  !!env['HTTP_X_AMZN_TRACE_ID'] && !on_cloud9
end

#polymorphic_functionObject



50
51
52
53
54
# File 'lib/jets/controller/middleware/local.rb', line 50

def polymorphic_function
  # Abusing PolyFun to run polymorphic code, should call LambdaExecutor directly
  # after reworking LambdaExecutor so it has a better interface.
  Jets::PolyFun.new(@controller.class, @meth)
end

#polymorphic_function?Boolean

Never hit when calling polymorphic function on AWS Lambda. Can only get called with the local server.

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/jets/controller/middleware/local.rb', line 45

def polymorphic_function?
  return false if ENV['_HANDLER'] # slight speed improvement on Lambda
  polymorphic_function.task.lang != :ruby
end

#routes_error_message(env) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/jets/controller/middleware/local.rb', line 78

def routes_error_message(env)
  message = "<h2>404 Error: Route #{env['PATH_INFO'].sub('/','')} not found</h2>"
  if Jets.env != "production"
    message << "<p>Here are the routes defined in your application:</p>"
    message << "#{routes_table}"
  end
  message
end

#routes_tableObject

Show pretty route table for user to help with debugging in non-production mode



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jets/controller/middleware/local.rb', line 105

def routes_table
  routes = Jets::Router.routes

  return "Your routes table is empty." if routes.empty?

  text = "Verb | Path | Controller#action\n"
  text << "--- | --- | ---\n"
  routes.each do |route|
    text << "#{route.method} | #{route.path} | #{route.to}\n"
  end
  html = Kramdown::Document.new(text).to_html
  puts html
  html
end

#run_polymophic_functionObject



57
58
59
60
61
62
63
# File 'lib/jets/controller/middleware/local.rb', line 57

def run_polymophic_function
  resp = polymorphic_function.run(@event, @meth) # polymorphic code
  status = resp['statusCode']
  headers = resp['headers']
  body = StringIO.new(resp['body'])
  [status, headers, body] # triplet
end