Created
July 18, 2013 17:44
-
-
Save dalecaru/6031348 to your computer and use it in GitHub Desktop.
Run static site with rack
This file contains hidden or 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
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