class Spokes::Middleware::ServiceName
Validates inbound and sets outbound Service-Name HTTP headers.
Usage:
class Application < Rails::Application config.middleware.use Spokes::Middleware::ServiceName end
Constants
- HEADER_NAME
- PATTERN
Public Class Methods
new(app, service_name:, exclude_paths: [])
click to toggle source
# File lib/spokes/middleware/service_name.rb, line 18 def initialize(app, service_name:, exclude_paths: []) raise "invalid name: #{service_name}" unless service_name =~ PATTERN @app = app @service_name = service_name @exclude_paths = path_to_regex(exclude_paths) end
Public Instance Methods
call(env)
click to toggle source
# File lib/spokes/middleware/service_name.rb, line 25 def call(env) begin unless exclude?(env['PATH_INFO'].chomp('/')) validate_header_presence(env: env, header_name: HEADER_NAME) validate_header_pattern(env: env, header_name: HEADER_NAME, pattern: PATTERN) end rescue Middleware::Concerns::HeaderValidation::NotValid => e return bad_request(e.message) end status, headers, body = @app.call(env) headers[HEADER_NAME] = @service_name [status, headers, body] end
Private Instance Methods
exclude?(path)
click to toggle source
# File lib/spokes/middleware/service_name.rb, line 42 def exclude?(path) @exclude_paths.each do |regex| return true if regex.match?(path) end false end
path_to_regex(exclude_paths)
click to toggle source
build out regular expression to match exclude path
# File lib/spokes/middleware/service_name.rb, line 50 def path_to_regex(exclude_paths) reg_ex_path = [] exclude_paths.each do |path| path_parts = path.split('/') regex_paths = path_parts.inject do |out, p| val = p val = '(\\S*)' if p.include?(':') out + '[/]' + val end reg_ex_path.push(Regexp.new(regex_paths)) end reg_ex_path end