Class: Spokes::Middleware::Health

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

Overview

Provides ‘/status` route.

Example setup:

class Application < Rails::Application
  config.middleware.use Spokes::Middleware::Health
end

Example responses:

$ curl -v -L http://localhost:3000/status
> GET /status HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Cache-Control: must-revalidate,no-cache,no-store
< X-Request-Id: 8916fc91-b773-4002-9a9a-59870894715c
< X-Runtime: 0.001055
< Transfer-Encoding: chunked
<
OK

$ curl -v -L http://localhost:3000/status -H "Content-Type: application/json"
> GET /status HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:3000
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Cache-Control: must-revalidate,no-cache,no-store
< X-Request-Id: 0a598c48-ca37-4b61-9677-20d8a8a4d637
< X-Runtime: 0.001252
< Transfer-Encoding: chunked
<
{"status":"OK"}

Constant Summary collapse

PATH =
'/status'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Health

Returns a new instance of Health.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/spokes/middleware/health.rb', line 47

def initialize(app, options = {})
  @app = app
  @options = {
    content_type: { simple: 'text/plain', details: 'application/json' },
    fail_if: -> { false },
    simple: ->(healthy) { healthy ? 'OK' : 'FAIL' },
    details: ->(healthy) { healthy ? { 'status': 'OK' }.to_json : { 'status': 'FAIL' }.to_json },
    status_code: ->(healthy) { healthy ? 200 : 503 },
    headers: lambda do |content_type|
      { 'Content-Type' => content_type, 'Cache-Control' => 'must-revalidate,no-cache,no-store' }
    end
  }.merge(options)
end

Instance Method Details

#call(env) ⇒ Object



61
62
63
64
65
66
# File 'lib/spokes/middleware/health.rb', line 61

def call(env)
  return @app.call(env) unless env['REQUEST_METHOD'] == 'GET' && env['PATH_INFO'] == PATH
  healthy = !@options[:fail_if].call
  status = get_status(env, healthy)
  [status[:status], status[:headers], status[:body]]
end