rack-maintenance_mode 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p290@maintenance_mode"
8
+
9
+ #
10
+ # Uncomment following line if you want options to be set only for given project.
11
+ #
12
+ # PROJECT_JRUBY_OPTS=( --1.9 )
13
+ #
14
+ # The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
15
+ #
16
+ # chmod +x ${rvm_path}/hooks/after_use_jruby_opts
17
+ #
18
+
19
+ #
20
+ # First we attempt to load the desired environment directly from the environment
21
+ # file. This is very fast and efficient compared to running through the entire
22
+ # CLI and selector. If you want feedback on which environment was used then
23
+ # insert the word 'use' after --create as this triggers verbose mode.
24
+ #
25
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
26
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
27
+ then
28
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
29
+
30
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
31
+ then
32
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
33
+ fi
34
+ else
35
+ # If the environment file has not yet been created, use the RVM CLI to select.
36
+ if ! rvm --create "$environment_id"
37
+ then
38
+ echo "Failed to create RVM environment '${environment_id}'."
39
+ return 1
40
+ fi
41
+ fi
42
+
43
+ #
44
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
45
+ # it be automatically loaded. Uncomment the following and adjust the filename if
46
+ # necessary.
47
+ #
48
+ # filename=".gems"
49
+ # if [[ -s "$filename" ]]
50
+ # then
51
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
52
+ # fi
53
+
54
+ # If you use bundler, this might be useful to you:
55
+ # if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
56
+ # then
57
+ # printf "The rubygem 'bundler' is not installed. Installing it now.\n"
58
+ # gem install bundler
59
+ # fi
60
+ # if [[ -s Gemfile ]] && command -v bundle
61
+ # then
62
+ # bundle install
63
+ # fi
64
+
65
+ if [[ $- == *i* ]] # check for interactive shells
66
+ then
67
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
68
+ else
69
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
70
+ fi
71
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in maintenance_mode.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module Rack
2
+ module MaintenanceMode
3
+ module DefaultModeCheck
4
+ def self.call(env)
5
+ ENV["MAINTENANCE"].to_s =~ /^(?:on|yes|y|true|t|enabled?)$/i
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ require 'rack/maintenance_mode/default_mode_check'
2
+
3
+ module Rack
4
+ module MaintenanceMode
5
+ ##
6
+ # A Rack middleware to automatically put the Rack application into maintenance
7
+ # mode (HTTP 503).
8
+ #
9
+ # This middleware allows you to optionally allow users to pass through the 503
10
+ # wall and access the application by IP or by route requested.
11
+ #
12
+ class Middleware
13
+ DEFAULT_RESPONSE = Proc.new { |env| [503, {"Content-Type" => "text/html"}, ["<html><body><h2>We are undergoing maintenance right now, please try again soon.</body></html>"]] }
14
+
15
+ def initialize(app, options = {})
16
+ @app = app
17
+ @mode_check = options[:if] || DefaultModeCheck
18
+ @response = options[:response] || DEFAULT_RESPONSE
19
+ end
20
+
21
+ def call(env)
22
+ maintenance_mode?(env) ? maintenance_response(env) : standard_response(env)
23
+ end
24
+
25
+
26
+ private
27
+
28
+
29
+ def maintenance_mode?(env)
30
+ @mode_check.call(env)
31
+ end
32
+
33
+ def maintenance_response(env)
34
+ @response.call(env)
35
+ end
36
+
37
+ def standard_response(env)
38
+ @app.call(env)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ module MaintenanceMode
3
+ class Railtie < Rails::Railtie
4
+ config.maintenance_mode = ActiveSupport::OrderedOptions.new
5
+
6
+ initializer 'rack-maintenance_mode.insert_middleware' do |app|
7
+ app.config.middleware.use 'Rack::MaintenanceMode::Middleware', config.maintenance_mode
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module MaintenanceMode
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require "rack/maintenance_mode/version"
2
+ require 'rack/maintenance_mode/middleware'
3
+
4
+ require 'rack/maintenance_mode/railtie' if defined?(Rails)
5
+
6
+ module Rack
7
+ module MaintenanceMode
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'rack/maintenance_mode'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/maintenance_mode/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-maintenance_mode"
7
+ s.version = Rack::MaintenanceMode::VERSION
8
+ s.authors = ["Nathaniel Bibler"]
9
+ s.email = ["git@nathanielbibler.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Manage maintenance mode for your application}
12
+ s.description = %q{Allows you to create a customized maintenance mode for your application that is run early in the Rack stack.}
13
+
14
+ s.rubyforge_project = "maintenance_mode"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_runtime_dependency "rack"
23
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Rack::MaintenanceMode do
5
+ let(:options) { Hash.new }
6
+ subject { app(options).call({}) }
7
+
8
+
9
+ context 'using the default settings' do
10
+ maintain_env "MAINTENANCE"
11
+
12
+ context "with ENV[\"MAINTENANCE\"] enabled" do
13
+ it 'should be in maintenance mode' do
14
+ ['t', 'true', 'enable', 'enabled', 'y', 'yes', "yes\n"].each do |setting|
15
+ ENV['MAINTENANCE'] = setting
16
+ app.call({}).should be_a_maintenance_response, "Failed with #{setting.inspect}"
17
+ end
18
+ end
19
+
20
+ it 'should contain the default response' do
21
+ ['t', 'true', 'enable', 'enabled', 'y', 'yes', "yes\n"].each do |setting|
22
+ ENV['MAINTENANCE'] = setting
23
+ app.call({}).should contain_the_default_response
24
+ end
25
+ end
26
+ end
27
+
28
+ context 'with ENV["MAINTENANCE"] disabled' do
29
+ before(:each) { ENV['MAINTENANCE'] = 'false' }
30
+
31
+ it { should_not be_a_maintenance_response }
32
+
33
+ it 'should return the response content' do
34
+ subject.last.should == ['Ok']
35
+ end
36
+ end
37
+
38
+ context 'with ENV["MAINTENANCE"] being nil' do
39
+ before(:each) { ENV['MAINTENANCE'] = nil }
40
+
41
+ it { should_not be_a_maintenance_response }
42
+
43
+ it 'should return the response content' do
44
+ subject.last.should == ['Ok']
45
+ end
46
+ end
47
+ end
48
+
49
+
50
+ context 'with a custom mode check (if)' do
51
+ context 'when the custom check returns truthie' do
52
+ let(:options) { {:if => -> env { true }} }
53
+ it { should be_a_maintenance_response }
54
+ it { should contain_the_default_response }
55
+ end
56
+
57
+ context 'when the custom check returns false' do
58
+ let(:options) { {:if => -> env { false }} }
59
+
60
+ it { should_not be_a_maintenance_response }
61
+
62
+ it 'should return the response content' do
63
+ subject.last.should == ['Ok']
64
+ end
65
+ end
66
+ end
67
+
68
+
69
+ context 'with a custom template' do
70
+ maintain_env "MAINTENANCE"
71
+ let(:options) { {response: -> env { [503, {'Content-Type' => 'text/plain'}, ['Custom']] } } }
72
+
73
+ context 'with maintenance enabled' do
74
+ before(:each) { ENV['MAINTENANCE'] = 'true' }
75
+
76
+ it 'should return the custom response' do
77
+ subject.last.should == ['Custom']
78
+ end
79
+ end
80
+
81
+ context 'with maintenance disabled' do
82
+ before(:each) { ENV['MAINTENANCE'] = 'false' }
83
+
84
+ it 'should return the original response' do
85
+ subject.last.should == ['Ok']
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ def app(options = {})
92
+ Rack::Builder.new {
93
+ use Rack::MaintenanceMode::Middleware, options
94
+ run -> env { [200, {'Content-Type' => 'text/plain'}, ['Ok']] }
95
+ }
96
+ end
97
+ end
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'rack/maintenance_mode'
3
+ require 'rack'
4
+
5
+ Dir[File.expand_path("../support", __FILE__) + "/**/*.rb"].each { |f| require f }
6
+
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :be_a_maintenance_response do |expected|
2
+ match do |actual|
3
+ actual.first == 503
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :contain_the_default_response do |expected|
2
+ match do |actual|
3
+ actual.last == Rack::MaintenanceMode::Middleware::DEFAULT_RESPONSE.call({}).last
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module MaintainEnv
2
+ def maintain_env(*keys)
3
+ before(:each) do
4
+ @_maintain_env = Hash.new
5
+ keys.each do |key|
6
+ @_maintain_env[key] = ENV[key]
7
+ end
8
+ end
9
+
10
+ after(:each) do
11
+ @_maintain_env.each_pair do |key, value|
12
+ ENV[key] = value
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.extend MaintainEnv
20
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-maintenance_mode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathaniel Bibler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2151951400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2151951400
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack
27
+ requirement: &2151950980 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2151950980
36
+ description: Allows you to create a customized maintenance mode for your application
37
+ that is run early in the Rack stack.
38
+ email:
39
+ - git@nathanielbibler.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .rspec
46
+ - .rvmrc
47
+ - Gemfile
48
+ - Rakefile
49
+ - lib/rack-maintenance_mode.rb
50
+ - lib/rack/maintenance_mode.rb
51
+ - lib/rack/maintenance_mode/default_mode_check.rb
52
+ - lib/rack/maintenance_mode/middleware.rb
53
+ - lib/rack/maintenance_mode/railtie.rb
54
+ - lib/rack/maintenance_mode/version.rb
55
+ - rack-maintenance_mode.gemspec
56
+ - spec/maintenance_spec.rb
57
+ - spec/spec_helper.rb
58
+ - spec/support/be_a_maintenance_response.rb
59
+ - spec/support/contain_the_default_response.rb
60
+ - spec/support/maintain_env.rb
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project: maintenance_mode
81
+ rubygems_version: 1.8.10
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Manage maintenance mode for your application
85
+ test_files:
86
+ - spec/maintenance_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/support/be_a_maintenance_response.rb
89
+ - spec/support/contain_the_default_response.rb
90
+ - spec/support/maintain_env.rb