health_rack 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +7 -0
- data/health_rack.gemspec +25 -0
- data/lib/health_rack/check.rb +47 -0
- data/lib/health_rack/configuration.rb +16 -0
- data/lib/health_rack/controller.rb +32 -0
- data/lib/health_rack/dsl.rb +24 -0
- data/lib/health_rack/middleware.rb +23 -0
- data/lib/health_rack/renderers/html_renderer.rb +64 -0
- data/lib/health_rack/renderers/json_renderer.rb +45 -0
- data/lib/health_rack/renderers.rb +10 -0
- data/lib/health_rack/version.rb +3 -0
- data/lib/health_rack.rb +14 -0
- data/spec/lib/health_rack/check_spec.rb +78 -0
- data/spec/lib/health_rack/configuration_spec.rb +36 -0
- data/spec/lib/health_rack/controller_spec.rb +46 -0
- data/spec/lib/health_rack/dsl_spec.rb +45 -0
- data/spec/lib/health_rack/middleware_spec.rb +33 -0
- data/spec/lib/health_rack/renderers/html_renderer_spec.rb +35 -0
- data/spec/lib/health_rack/renderers/json_renderer_spec.rb +36 -0
- data/spec/lib/health_rack_spec.rb +12 -0
- data/spec/spec_helper.rb +2 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f46a84c075053e1c303e2853ab00d5fbab262ad
|
4
|
+
data.tar.gz: c81c644d0dc3eeff6debead6dc5619baeeeeaf9b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af1d6f828eb8b4328eafd35be3fc2463d7ed10c1a3c4e61e3d36e8b687e951b37b864cc479739411e1ce297e28c566fb8d09b8fc39f1dd1c346742ed795ab680
|
7
|
+
data.tar.gz: e9a5a40231bc7acfd5de4aa242159dfaa5a854b7aa1b4c95819eb31c24e96c9987010ef19d37946b5bb9d1816cb4d2d94dbc30bf1a52aa810b6afe627fea829b
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Marty Zalega
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# HealthRack
|
2
|
+
|
3
|
+
Easily add a health status page to your rack-compatible application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'health_rack'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install health_rack
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
use HealthRack, path: "/status" do
|
25
|
+
check "Database" do
|
26
|
+
# Add code to check connectivity to database
|
27
|
+
end
|
28
|
+
|
29
|
+
check "Lemons" do
|
30
|
+
true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Then simply visit the page (ie. `/status`) to view the output. By default the output will be in HTML but you can add the `.json` suffix to get a JSON output instead.
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it ( https://github.com/everydayhero/health_rack/fork )
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/health_rack.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'health_rack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "health_rack"
|
8
|
+
spec.version = HealthRack::VERSION
|
9
|
+
spec.authors = ["Marty Zalega"]
|
10
|
+
spec.email = ["marty@zalega.me"]
|
11
|
+
spec.summary = %q{A rack middleware for an easy health check endpoint}
|
12
|
+
# spec.description = %q{TODO: Write a longer description. Optional.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rack", "~> 1.5.2"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "benchmark"
|
2
|
+
|
3
|
+
module HealthRack
|
4
|
+
class Check
|
5
|
+
attr_reader :title, :value, :duration
|
6
|
+
|
7
|
+
def initialize(title, &proc)
|
8
|
+
@title = title || raise(ArgumentError, "A title must be specified")
|
9
|
+
@proc = proc || raise(ArgumentError, "A block must be specified")
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
measure
|
14
|
+
value
|
15
|
+
end
|
16
|
+
|
17
|
+
def status
|
18
|
+
case value
|
19
|
+
when nil, 0, false, Exception then false
|
20
|
+
else true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method :status?, :status
|
25
|
+
|
26
|
+
def error?
|
27
|
+
value.is_a? Exception
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def call
|
33
|
+
@value = begin
|
34
|
+
@proc.call
|
35
|
+
rescue Exception => ex
|
36
|
+
# Need to log the exception
|
37
|
+
ex
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def measure
|
42
|
+
@duration = Benchmark.realtime do
|
43
|
+
call
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module HealthRack
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :title, :path
|
4
|
+
attr_reader :checks
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@checks = []
|
8
|
+
|
9
|
+
options.each do |name, value|
|
10
|
+
method_name = "#{name}="
|
11
|
+
public_send(method_name, value) if respond_to?(method_name, false)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "rack/request"
|
2
|
+
require "rack/response"
|
3
|
+
require "rack/body_proxy"
|
4
|
+
|
5
|
+
module HealthRack
|
6
|
+
class Controller
|
7
|
+
def initialize(env, config)
|
8
|
+
@config = config
|
9
|
+
@request = Rack::Request.new(env)
|
10
|
+
@response = Rack::Response.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
@response['Content-type'] = renderer.content_type
|
15
|
+
renderer.render(@response)
|
16
|
+
@response.finish
|
17
|
+
end
|
18
|
+
|
19
|
+
def format
|
20
|
+
@format ||= begin
|
21
|
+
md = @request.path_info.match(/\.([a-z]+)$/i) || []
|
22
|
+
md[1] || 'html'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def renderer
|
29
|
+
@renderer ||= Renderers.find(format).new(@config)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module HealthRack
|
2
|
+
class DSL
|
3
|
+
def initialize(config, &block)
|
4
|
+
@config = config
|
5
|
+
|
6
|
+
if block_given?
|
7
|
+
case block.arity
|
8
|
+
when 0 then instance_eval(&block)
|
9
|
+
when 1, -1 then yield(self)
|
10
|
+
else raise(ArgumentError, "wrong number of arguments 0..1")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def title(title)
|
16
|
+
@config.title = title
|
17
|
+
end
|
18
|
+
|
19
|
+
def check(*args, &block)
|
20
|
+
@config.checks << Check.new(*args, &block)
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module HealthRack
|
2
|
+
class Middleware
|
3
|
+
DEFAULT_OPTIONS = {
|
4
|
+
title: "Health check",
|
5
|
+
path: "/health",
|
6
|
+
}.freeze
|
7
|
+
|
8
|
+
def initialize(app, options = {}, &block)
|
9
|
+
@app = app
|
10
|
+
@config = Configuration.new(DEFAULT_OPTIONS.merge(options))
|
11
|
+
|
12
|
+
DSL.new(@config, &block) if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
if @config.path.start_with?(env['PATH_INFO'])
|
17
|
+
Controller.new(env, @config).perform
|
18
|
+
else
|
19
|
+
@app.call(env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module HealthRack
|
2
|
+
module Renderers
|
3
|
+
class HTMLRenderer
|
4
|
+
CONTENT_TYPE = "text/html; charset=UTF-8"
|
5
|
+
|
6
|
+
def initialize(config)
|
7
|
+
@config = config
|
8
|
+
end
|
9
|
+
|
10
|
+
def content_type
|
11
|
+
CONTENT_TYPE
|
12
|
+
end
|
13
|
+
|
14
|
+
def render(buffer)
|
15
|
+
buffer.write <<-HTML
|
16
|
+
<doctype html>
|
17
|
+
<html>
|
18
|
+
<head>
|
19
|
+
<meta charset="utf-8">
|
20
|
+
<title>#{@config.title}</title>
|
21
|
+
</head>
|
22
|
+
<body>
|
23
|
+
<table border="1">
|
24
|
+
<caption>#{@config.title}</caption>
|
25
|
+
<thead>
|
26
|
+
<tr>
|
27
|
+
<th> </th>
|
28
|
+
<th>Status</th>
|
29
|
+
<th>Time</th>
|
30
|
+
</tr>
|
31
|
+
</thead>
|
32
|
+
<tbody>
|
33
|
+
HTML
|
34
|
+
|
35
|
+
@config.checks.each do |check|
|
36
|
+
buffer.write <<-HTML
|
37
|
+
<tr>
|
38
|
+
<th>#{check.title}</th>
|
39
|
+
<td>#{status(check)}</td>
|
40
|
+
<td>#{check.duration.to_f * 1000}</td>
|
41
|
+
</tr>
|
42
|
+
HTML
|
43
|
+
end
|
44
|
+
|
45
|
+
buffer.write <<-HTML
|
46
|
+
</tbody>
|
47
|
+
</table>
|
48
|
+
</body>
|
49
|
+
</html>
|
50
|
+
HTML
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def status(check)
|
56
|
+
case
|
57
|
+
when check.error? then 'ERROR'
|
58
|
+
when check.status? then 'OK'
|
59
|
+
else 'FAIL'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module HealthRack
|
4
|
+
module Renderers
|
5
|
+
class JSONRenderer
|
6
|
+
CONTENT_TYPE = "application/json"
|
7
|
+
|
8
|
+
def initialize(config)
|
9
|
+
@config = config
|
10
|
+
end
|
11
|
+
|
12
|
+
def content_type
|
13
|
+
CONTENT_TYPE
|
14
|
+
end
|
15
|
+
|
16
|
+
def render(buffer)
|
17
|
+
buffer.write(json)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def json
|
23
|
+
JSON.generate(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def data
|
27
|
+
{
|
28
|
+
title: @config.title,
|
29
|
+
status: overall_status,
|
30
|
+
summary: summary
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def overall_status
|
35
|
+
@config.checks.all?(&:status)
|
36
|
+
end
|
37
|
+
|
38
|
+
def summary
|
39
|
+
@config.checks.map do |check|
|
40
|
+
{title: check.title, status: check.status, duration: check.duration}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/health_rack.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "health_rack/version"
|
2
|
+
|
3
|
+
module HealthRack
|
4
|
+
autoload :Configuration, "health_rack/configuration"
|
5
|
+
autoload :DSL, "health_rack/dsl"
|
6
|
+
autoload :Check, "health_rack/check"
|
7
|
+
autoload :Controller, "health_rack/controller"
|
8
|
+
autoload :Middleware, "health_rack/middleware"
|
9
|
+
autoload :Renderers, "health_rack/renderers"
|
10
|
+
|
11
|
+
def self.new(*args, &block)
|
12
|
+
Middleware.new(*args, &block)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::Check do
|
4
|
+
describe "initialization" do
|
5
|
+
it "should raise an error when no title is given" do
|
6
|
+
expect{ HealthRack::Check.new(nil) }.to raise_error(ArgumentError)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should raise an error when no block is given" do
|
10
|
+
expect{ HealthRack::Check.new("test") }.to raise_error(ArgumentError)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
{"an object" => Object.new, "a one" => 1, "true" => true}.each do |desc, value|
|
15
|
+
describe "performing a successful check returning #{desc}" do
|
16
|
+
let(:check){ HealthRack::Check.new("test"){ value } }
|
17
|
+
|
18
|
+
before(:each) do
|
19
|
+
check.perform
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set value to the return value of proc" do
|
23
|
+
expect(check.value).to eq(value)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should measure the duration of the check" do
|
27
|
+
expect(check.duration).not_to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should mark the status true" do
|
31
|
+
expect(check.status).to be(true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
{"nil" => nil, "zero" => 0, "false" => false}.each do |desc, value|
|
37
|
+
describe "performing a failed check returning #{desc}" do
|
38
|
+
let(:check){ HealthRack::Check.new("test"){ value } }
|
39
|
+
|
40
|
+
before(:each) do
|
41
|
+
check.perform
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should set value to the return value of proc" do
|
45
|
+
expect(check.value).to eq(value)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should measure the duration of the check" do
|
49
|
+
expect(check.duration).not_to be_nil
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should mark the status false" do
|
53
|
+
expect(check.status).to be(false)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "performing a check that raises an error" do
|
59
|
+
let(:error){ Exception.new("oi") }
|
60
|
+
let(:check){ HealthRack::Check.new("test"){ raise error } }
|
61
|
+
|
62
|
+
before(:each) do
|
63
|
+
check.perform
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set the value to the exception" do
|
67
|
+
expect(check.value).to be(error)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should measure the duration of the check" do
|
71
|
+
expect(check.duration).not_to be_nil
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should mark the status false" do
|
75
|
+
expect(check.status).to be(false)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::Configuration do
|
4
|
+
describe "initializing with no arguments" do
|
5
|
+
let(:config){ HealthRack::Configuration.new }
|
6
|
+
|
7
|
+
it "should default title to nil" do
|
8
|
+
expect(config.title).to be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should default path to nil" do
|
12
|
+
expect(config.path).to be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should default checks to an empty array" do
|
16
|
+
expect(config.checks).to eq([])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "initializing with options" do
|
21
|
+
let(:options){ {title: "Test", path: "/test", checks: [:foo]} }
|
22
|
+
let(:config){ HealthRack::Configuration.new(options) }
|
23
|
+
|
24
|
+
it "should set title" do
|
25
|
+
expect(config.title).to eq("Test")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should set path" do
|
29
|
+
expect(config.path).to eq("/test")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "shouldn't set checks" do
|
33
|
+
expect(config.checks).to eq([])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::Controller do
|
4
|
+
let(:config){ HealthRack::Configuration.new }
|
5
|
+
|
6
|
+
describe "responding" do
|
7
|
+
let(:controller){ HealthRack::Controller.new({}, config) }
|
8
|
+
let(:response){ controller.perform }
|
9
|
+
let(:headers){ response[1] }
|
10
|
+
let(:body){ response[2].body.join }
|
11
|
+
|
12
|
+
it "should return a rack-compatible response" do
|
13
|
+
expect(response[0]).to be_kind_of(Fixnum)
|
14
|
+
expect(response[1]).to be_kind_of(Hash)
|
15
|
+
expect(response[2]).to respond_to(:each)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should set the content header" do
|
19
|
+
expect(headers).to include("Content-type" => "text/html; charset=UTF-8")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should render to the body" do
|
23
|
+
expect(body).not_to be_empty
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "the request's format when has an extension" do
|
28
|
+
let(:env){ Hash.new }
|
29
|
+
let(:controller){ HealthRack::Controller.new(env, config) }
|
30
|
+
|
31
|
+
it "should return the extension" do
|
32
|
+
env.update "PATH_INFO" => "/test.json"
|
33
|
+
expect(controller.format).to eq('json')
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return the default extension when none specified" do
|
37
|
+
env.update "PATH_INFO" => "/test"
|
38
|
+
expect(controller.format).to eq('html')
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return the default extension when is invalid" do
|
42
|
+
env.update "PATH_INFO" => "/test.json/"
|
43
|
+
expect(controller.format).to eq('html')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::DSL do
|
4
|
+
let(:config){ HealthRack::Configuration.new }
|
5
|
+
|
6
|
+
describe "initializing without a block" do
|
7
|
+
it "should not raise argument error" do
|
8
|
+
expect{ HealthRack::DSL.new(config) }.not_to raise_error
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "initialize with a block that has one argument" do
|
13
|
+
it "should call block passing itself as an argument" do
|
14
|
+
expect{|b| HealthRack::DSL.new(config, &b) }.to yield_with_args(HealthRack::DSL)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "initialize with a block that has no arguments" do
|
19
|
+
it "should call the block in the context of itself"
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "initialize with a block that has more than one argument" do
|
23
|
+
it "should raise an error" do
|
24
|
+
expect{ HealthRack::DSL.new(config){|a, b|} }.to raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "setting the title" do
|
29
|
+
let(:dsl){ HealthRack::DSL.new(config) }
|
30
|
+
|
31
|
+
it "should set the title on the config" do
|
32
|
+
dsl.title "example"
|
33
|
+
expect(config.title).to eq("example")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "adding a check" do
|
38
|
+
let(:dsl){ HealthRack::DSL.new(config) }
|
39
|
+
|
40
|
+
it "should add a new check to the config" do
|
41
|
+
dsl.check("Test"){ true }
|
42
|
+
expect(config.checks.first).to be_kind_of(HealthRack::Check)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::Middleware do
|
4
|
+
describe "initialization" do
|
5
|
+
let(:app){ lambda{|env|} }
|
6
|
+
|
7
|
+
it "should call block when block is given" do
|
8
|
+
expect{|b| HealthRack::Middleware.new(app, {}, &b) }.to yield_control
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "calling the middleware" do
|
13
|
+
let(:env){ {"PATH_INFO" => "/test"} }
|
14
|
+
|
15
|
+
it "should respond when path info matches the path" do
|
16
|
+
expect do |block|
|
17
|
+
HealthRack::Middleware.new(Proc.new(&block), {path: "/test"}).call(env)
|
18
|
+
end.not_to yield_with_args(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should respond when path info matches the path with the format" do
|
22
|
+
expect do |block|
|
23
|
+
HealthRack::Middleware.new(Proc.new(&block), {path: "/test.html"}).call(env)
|
24
|
+
end.not_to yield_with_args(env)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should call the app when path info doesn't match the path" do
|
28
|
+
expect do |block|
|
29
|
+
HealthRack::Middleware.new(Proc.new(&block), {}).call(env)
|
30
|
+
end.to yield_with_args(env)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::Renderers::HTMLRenderer do
|
4
|
+
class Buffer < Array
|
5
|
+
alias_method :write, :push
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
join(" ")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:check){ HealthRack::Check.new("Foobar"){ true } }
|
13
|
+
let(:config){ HealthRack::Configuration.new(title: "Test") }
|
14
|
+
let(:buffer){ Buffer.new }
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
check.perform
|
18
|
+
config.checks << check
|
19
|
+
HealthRack::Renderers::HTMLRenderer.new(config).render(buffer)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "rendering" do
|
23
|
+
it "should include the title in the header" do
|
24
|
+
expect(buffer.to_s).to match(/<title>\s*Test\s*<\/title>/)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should include the title in the table caption" do
|
28
|
+
expect(buffer.to_s).to match(/<caption>\s*Test\s*<\/caption>/)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should render checks" do
|
32
|
+
expect(buffer.to_s).to match(/<tr>\s*<th>Foobar<\/th>\s*<td>OK<\/td>\s*<td>\d+\.\d+<\/td>\s*<\/tr>/i)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack::Renderers::JSONRenderer do
|
4
|
+
class Buffer < Array
|
5
|
+
alias_method :write, :push
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
join(" ")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:check){ HealthRack::Check.new("Foobar"){ true } }
|
13
|
+
let(:config){ HealthRack::Configuration.new(title: "Test") }
|
14
|
+
let(:buffer){ Buffer.new }
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
check.perform
|
18
|
+
config.checks << check
|
19
|
+
HealthRack::Renderers::JSONRenderer.new(config).render(buffer)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "rendering" do
|
23
|
+
it "should include the title" do
|
24
|
+
expect(buffer.to_s).to match(/"title":\s*"Test"/)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should include the overall status" do
|
28
|
+
expect(buffer.to_s).to match(/"status":\s*true/)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should render checks" do
|
32
|
+
expect(buffer.to_s).to match(/{\s*"title":\s*"Foobar",\s*"status":\s*true,\s*"duration":\s*\d+/)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HealthRack do
|
4
|
+
describe "initializing" do
|
5
|
+
let(:app){ lambda{} }
|
6
|
+
let(:health_rack){ HealthRack.new(app) }
|
7
|
+
|
8
|
+
it "should return a new instance of the middleware" do
|
9
|
+
expect(health_rack).to be_kind_of(HealthRack::Middleware)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: health_rack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marty Zalega
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.5.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- marty@zalega.me
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- health_rack.gemspec
|
69
|
+
- lib/health_rack.rb
|
70
|
+
- lib/health_rack/check.rb
|
71
|
+
- lib/health_rack/configuration.rb
|
72
|
+
- lib/health_rack/controller.rb
|
73
|
+
- lib/health_rack/dsl.rb
|
74
|
+
- lib/health_rack/middleware.rb
|
75
|
+
- lib/health_rack/renderers.rb
|
76
|
+
- lib/health_rack/renderers/html_renderer.rb
|
77
|
+
- lib/health_rack/renderers/json_renderer.rb
|
78
|
+
- lib/health_rack/version.rb
|
79
|
+
- spec/lib/health_rack/check_spec.rb
|
80
|
+
- spec/lib/health_rack/configuration_spec.rb
|
81
|
+
- spec/lib/health_rack/controller_spec.rb
|
82
|
+
- spec/lib/health_rack/dsl_spec.rb
|
83
|
+
- spec/lib/health_rack/middleware_spec.rb
|
84
|
+
- spec/lib/health_rack/renderers/html_renderer_spec.rb
|
85
|
+
- spec/lib/health_rack/renderers/json_renderer_spec.rb
|
86
|
+
- spec/lib/health_rack_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A rack middleware for an easy health check endpoint
|
112
|
+
test_files:
|
113
|
+
- spec/lib/health_rack/check_spec.rb
|
114
|
+
- spec/lib/health_rack/configuration_spec.rb
|
115
|
+
- spec/lib/health_rack/controller_spec.rb
|
116
|
+
- spec/lib/health_rack/dsl_spec.rb
|
117
|
+
- spec/lib/health_rack/middleware_spec.rb
|
118
|
+
- spec/lib/health_rack/renderers/html_renderer_spec.rb
|
119
|
+
- spec/lib/health_rack/renderers/json_renderer_spec.rb
|
120
|
+
- spec/lib/health_rack_spec.rb
|
121
|
+
- spec/spec_helper.rb
|