class Racknga::Middleware::InstanceName
This is a middleware that adds “X-Responsed-By” header to responses. It’s useful to determine responded server when your Rack applications are deployed behind load balancers.
Usage:
require "racknga" use Racknga::Middleware::InstanceName run YourApplication
Constants
- CURRENT_BRANCH_MARKER
- DEFAULT_HEADER_NAME
Attributes
header[R]
Public Class Methods
new(application, options={})
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 31 def initialize(application, options={}) @application = application @options = options @header = construct_header.freeze @headers = construct_headers.freeze end
Public Instance Methods
application_name()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 50 def application_name @options[:application_name] || @application.class.name end
branch()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 71 def branch current_branch = nil `git branch -a`.each_line do |line| case line when CURRENT_BRANCH_MARKER current_branch = line.sub(CURRENT_BRANCH_MARKER, "").strip break end end current_branch end
call(environment)
click to toggle source
For Rack.
# File lib/racknga/middleware/instance_name.rb, line 40 def call(environment) response = @application.call(environment).to_a [ response[0], response[1].merge(@headers), response[2], ] end
revision()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 58 def revision `git describe --abbrev=7 HEAD`.strip # XXX be SCM-agonostic end
ruby()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 83 def ruby RUBY_DESCRIPTION end
server()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 62 def server `hostname`.strip end
user()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 66 def user `id --user --name`.strip end
version()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 54 def version @options[:version] end
Private Instance Methods
construct_header()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 99 def construct_header format_header(format_application_name(application_name), format_version(version), format_revision(branch, revision), format_server(server), format_user(user), format_ruby(ruby)) end
construct_headers()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 93 def construct_headers { header_name => header, } end
format_application_name(name)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 112 def format_application_name(name) format_if_possible(name) do "#{name}" end end
format_branch(branch)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 130 def format_branch(branch) format_if_possible(branch) do " (#{branch})" end end
format_header(*arguments)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 108 def format_header(*arguments) arguments.compact.join(" ") end
format_if_possible(data) { || ... }
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 154 def format_if_possible(data) if data and (data.respond_to?(:to_s) and not data.to_s.empty?) yield else nil end end
format_revision(branch, revision)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 124 def format_revision(branch, revision) format_if_possible(revision) do "(at #{revision}#{format_branch(branch)})" end end
format_ruby(ruby)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 148 def format_ruby(ruby) format_if_possible(ruby) do "with #{ruby}" end end
format_server(server)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 136 def format_server(server) format_if_possible(server) do "on #{server}" end end
format_user(user)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 142 def format_user(user) format_if_possible(user) do "by #{user}" end end
format_version(version)
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 118 def format_version(version) format_if_possible(version) do "v#{version}" end end
header_name()
click to toggle source
# File lib/racknga/middleware/instance_name.rb, line 89 def header_name @options[:header_name] || DEFAULT_HEADER_NAME end