Permalink
Browse files

Rack::Backstage - maintenance page middleware

If specified file exists, serve that one. Convenient for
maintenance pages.
  • Loading branch information...
vangberg authored and rtomayko committed Feb 19, 2009
1 parent 43a508f commit 3e1d5f2d0fce4475a88e47d19e8532843bcdac86
Showing with 48 additions and 0 deletions.
  1. +2 −0 README.rdoc
  2. +20 −0 lib/rack/contrib/backstage.rb
  3. +1 −0 test/Maintenance.html
  4. +25 −0 test/spec_rack_backstage.rb
View
@@ -38,6 +38,8 @@ interface:
to Disk or a user provider Ruby object. Similar to Rails' page caching.
* Rack::RelativeRedirect - Transforms relative paths in redirects to
absolute URLs.
+* Rack::Backstage - Returns content of specified file if it exists, which makes
+ it convenient for putting up maintenance pages.
=== Use
@@ -0,0 +1,20 @@
+module Rack
+ class Backstage
+ File = ::File
+
+ def initialize(app, path)
+ @app = app
+ @file = File.expand_path(path)
+ end
+
+ def call(env)
+ if File.exists?(@file)
+ content = File.read(@file)
+ length = "".respond_to?(:bytesize) ? content.bytesize.to_s : content.size.to_s
+ [503, {'Content-Type' => 'text/html', 'Content-Length' => length}, content]
+ else
+ @app.call(env)
+ end
+ end
+ end
+end
View
@@ -0,0 +1 @@
+Under maintenance.
@@ -0,0 +1,25 @@
+require 'test/spec'
+require 'rack/mock'
+require 'rack/contrib/backstage'
+
+context "Rack::Backstage" do
+ specify "shows maintenances page if present" do
+ app = Rack::Builder.new do
+ use Rack::Backstage, 'test/Maintenance.html'
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
+ end
+ response = Rack::MockRequest.new(app).get('/')
+ response.body.should.equal('Under maintenance.')
+ response.status.should.equal(503)
+ end
+
+ specify "passes on request if page is not present" do
+ app = Rack::Builder.new do
+ use Rack::Backstage, 'test/Nonsense.html'
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
+ end
+ response = Rack::MockRequest.new(app).get('/')
+ response.body.should.equal('Hello, World!')
+ response.status.should.equal(200)
+ end
+end

0 comments on commit 3e1d5f2

Please sign in to comment.