Class: Vault::Web

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/vault-tools/web.rb

Overview

Base class for HTTP API services.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.route(verb, action) ⇒ Object

Store the action for logging purposes.



47
48
49
50
# File 'lib/vault-tools/web.rb', line 47

def route(verb, action, *)
  condition { @action = action }
  super
end

Instance Method Details

#/Object

Determine if the service is running and responding to requests.

Returns:

  • The following responses may be returned by this method:

    • HTTP 200 OK: Returned if the request was successful.


143
144
145
# File 'lib/vault-tools/web.rb', line 143

head_unprotected '/' do
  status(200)
end

#/healthObject

Determine if the service is running and responding to requests.

Returns:

  • The following responses may be returned by this method:

    • HTTP 200 OK: Returned if the request was successful with OK in the body.


154
155
156
# File 'lib/vault-tools/web.rb', line 154

get_unprotected '/health' do
  [200, 'OK']
end

#boomObject

Trigger an internal server error (to test monitoring and paging tools).

Returns:

  • The following responses may be returned by this method:

    • HTTP 500 Internal Server Error: Returned with a traceback in the body.


165
166
167
# File 'lib/vault-tools/web.rb', line 165

get_unprotected '/boom' do
  raise "An expected error occurred."
end

#call(env) ⇒ Object

Work with request id out of the box



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vault-tools/web.rb', line 10

def call(env)
  Thread.current[:request_id] = env['HTTP_X_REQUEST_ID'] || SecureRandom.uuid
  if defined? Excon
    decorate_excon!
    Excon.defaults[:headers]['X-Request-ID'] = Thread.current[:request_id]
  end
  env['HTTP_X_REQUEST_ID'] = Thread.current[:request_id]
  status, headers, response = super(env)
  headers['Request-ID'] = Thread.current[:request_id]
  [status, headers, response]
end

#decorate_excon!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vault-tools/web.rb', line 22

def decorate_excon!
  return unless defined? Excon
  begin
    return if Excon.request_id_decorated?
  rescue
    Excon.module_eval do
      def self.request_id_decorated?
        true
      end
    end
    Excon::Connection.class_eval do
      alias :stock_initialize :initialize
      def initialize(params={})
        stock_initialize(params)
        if Excon.defaults[:headers].key? 'X-Request-ID'
          @data[:headers]['X-Request-ID'] ||= Excon.defaults[:headers]['X-Request-ID']
        end
      end
    end
  end
end