Class: Vault::Web
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Vault::Web
- Defined in:
- lib/vault-tools/web.rb
Overview
Base class for HTTP API services.
Class Method Summary collapse
-
.route(verb, action) ⇒ Object
Store the action for logging purposes.
Instance Method Summary collapse
-
#/ ⇒ Object
Determine if the service is running and responding to requests.
-
#/health ⇒ Object
Determine if the service is running and responding to requests.
-
#boom ⇒ Object
Trigger an internal server error (to test monitoring and paging tools).
-
#call(env) ⇒ Object
Work with request id out of the box.
- #decorate_excon! ⇒ Object
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.
131 132 133 |
# File 'lib/vault-tools/web.rb', line 131 head_unprotected '/' do status(200) end |
#/health ⇒ Object
Determine if the service is running and responding to requests.
142 143 144 |
# File 'lib/vault-tools/web.rb', line 142 get_unprotected '/health' do [200, 'OK'] end |
#boom ⇒ Object
Trigger an internal server error (to test monitoring and paging tools).
153 154 155 |
# File 'lib/vault-tools/web.rb', line 153 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 |