Class: Racknga::Middleware::InstanceName — racknga - Ranguba

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

Constant Summary

CURRENT_BRANCH_MARKER =
/\A\* /
DEFAULT_HEADER_NAME =
"X-Responsed-By"

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (InstanceName) initialize(application, options = {})

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

- (Object) header (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

- (Object) application_name



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

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

- (Object) branch



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/racknga/middleware/instance_name.rb', line 72

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

- (Object) call(environment)

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

- (Object) revision



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

- (Object) ruby



84
85
86
# File 'lib/racknga/middleware/instance_name.rb', line 84

def ruby
  RUBY_DESCRIPTION
end

- (Object) server



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

def server
  `hostname`.strip
end

- (Object) user



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

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

- (Object) version



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

def version
  @options[:version]
end