Created
April 20, 2010 20:56
-
-
Save rgrove/373063 to your computer and use it in GitHub Desktop.
Simple "right here, right now" web server for testing static content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install dependencies: | |
# | |
# gem install thin | |
# | |
# Run this to serve the current directory at http://localhost:3000/: | |
# | |
# thin -R /path/to/serve.ru start | |
# | |
require 'rubygems' | |
require 'rack' | |
require 'rack/commonlogger' | |
require 'rack/conditionalget' | |
require 'rack/deflater' | |
require 'rack/directory' | |
require 'rack/etag' | |
DIR = File.expand_path('.') | |
class NoCache | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, body = @app.call(env) | |
headers['Cache-Control'] = 'must-revalidate' | |
[status, headers, body] | |
end | |
end | |
class Cache | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, body = @app.call(env) | |
headers['Cache-Control'] = 'max-age=315360000' | |
headers['Expires'] = 'Fri, 01 May 2020 03:47:24 GMT' | |
[status, headers, body] | |
end | |
end | |
use Rack::CommonLogger | |
use Rack::ConditionalGet | |
use Rack::Deflater | |
use Rack::ETag | |
# Enable or disable caching as you wish. | |
use NoCache | |
# use Cache | |
map '/' do | |
run Rack::Directory.new(DIR) | |
puts "Serving all files under #{DIR}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment