Class: Racknga::Middleware::InstanceName

Inherits:
Object
  • Object
show all
Defined in:
lib/racknga/middleware/instance_name.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, options = {}) ⇒ InstanceName

Returns a new instance of InstanceName.



32
33
34
35
36
37
38
# File 'lib/racknga/middleware/instance_name.rb', line 32

def initialize(application, options={})
  @application = application
  @options = options

  @header = construct_header.freeze
  @headers = construct_headers.freeze
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



31
32
33
# File 'lib/racknga/middleware/instance_name.rb', line 31

def header
  @header
end

Instance Method Details

#application_nameObject



51
52
53
# File 'lib/racknga/middleware/instance_name.rb', line 51

def application_name
  @options[:application_name] || @application.class.name
end

#call(environment) ⇒ Object

For Rack.



41
42
43
44
45
46
47
48
49
# File 'lib/racknga/middleware/instance_name.rb', line 41

def call(environment)
  response = @application.call(environment).to_a

  [
    response[0],
    response[1].merge(@headers),
    response[2],
  ]
end

#revisionObject



59
60
61
# File 'lib/racknga/middleware/instance_name.rb', line 59

def revision
  `git describe --abbrev=7 HEAD`.strip # XXX be SCM-agonostic
end

#serverObject



63
64
65
# File 'lib/racknga/middleware/instance_name.rb', line 63

def server
  `hostname`.strip
end

#userObject



67
68
69
# File 'lib/racknga/middleware/instance_name.rb', line 67

def user
  `id --user --name`.strip
end

#versionObject



55
56
57
# File 'lib/racknga/middleware/instance_name.rb', line 55

def version
  @options[:version]
end