Class: Rx::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rx/middleware.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  cache: true,
  authorization: nil,
  liveness_path: "/liveness",
  readiness_path: "/readiness",
  deep_path: "/deep"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, liveness: [Rx::Check::FileSystemCheck.new], readiness: [Rx::Check::FileSystemCheck.new], deep_critical: [], deep_secondary: [], options: {}) ⇒ Middleware

Returns a new instance of Middleware.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rx/middleware.rb', line 13

def initialize(app,
               liveness:       [Rx::Check::FileSystemCheck.new],
               readiness:      [Rx::Check::FileSystemCheck.new],
               deep_critical:  [],
               deep_secondary: [],
               options:        {})
  @app = app
  @options = DEFAULT_OPTIONS.merge(options)
  @cache = cache_factory(@options)

  @liveness_checks = liveness
  @readiness_checks = readiness
  @deep_critical_checks = deep_critical
  @deep_secondary_checks = deep_secondary
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rx/middleware.rb', line 29

def call(env)
  unless health_check_request?(path(env))
    return app.call(env)
  end

  case path(env)
  when @options[:liveness_path]
    ok = check_to_component(liveness_checks).map { |x| x[:status] == 200 }.all?
    liveness_response(ok)
  when @options[:readiness_path]
    readiness_response(check_to_component(readiness_checks))
  when @options[:deep_path]
    if !Rx::Util::HealthCheckAuthorization.new(env, @options[:authorization]).ok?
      deep_response_authorization_failed
    else
      @cache.cache("deep") do
        readiness = check_to_component(readiness_checks)
        critical  = check_to_component(deep_critical_checks)
        secondary = check_to_component(deep_secondary_checks)

        deep_response(readiness, critical, secondary)
      end
    end
  end
end