Permalink
Please sign in to comment.
Browse files
Rack::Backstage - maintenance page middleware
If specified file exists, serve that one. Convenient for maintenance pages.
- Loading branch information...
Showing
with
48 additions
and 0 deletions.
- +2 −0 README.rdoc
- +20 −0 lib/rack/contrib/backstage.rb
- +1 −0 test/Maintenance.html
- +25 −0 test/spec_rack_backstage.rb
@@ -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 |
@@ -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