Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dalecaru
Created July 18, 2013 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalecaru/6031348 to your computer and use it in GitHub Desktop.
Save dalecaru/6031348 to your computer and use it in GitHub Desktop.
Run static site with rack
APP_ROOT = ::File.expand_path('public', ::File.dirname(__FILE__))
require 'rack'
require 'rack/content_length'
require 'rack/file'
module Rack
class DirectoryIndex
def initialize(app, root)
@app = app
@root = root
end
def call(env)
index_path = ::File.join(@root, Rack::Request.new(env).path.split('/'), 'index.html')
if ::File.exists?(index_path)
return [200, {"Content-Type" => "text/html"}, [::File.read(index_path)]]
else
@app.call(env)
end
end
end
class NotFound
def initialize(app, path)
@app = app
@content = ::File.read(::File.expand_path(path))
end
def call(env)
resp = @app.call(env)
if 404 == resp[0]
resp = [404, {'Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => @content.size.to_s}, [@content]]
end
resp
end
end
end
use Rack::ContentLength
use Rack::NotFound, File.join(APP_ROOT, '404.html')
use Rack::DirectoryIndex, APP_ROOT
run Rack::File.new(APP_ROOT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment