Skip to content

Instantly share code, notes, and snippets.

@gedex
Created February 12, 2013 06:49
Show Gist options
  • Save gedex/4760680 to your computer and use it in GitHub Desktop.
Save gedex/4760680 to your computer and use it in GitHub Desktop.
rack-based static webserver
use Rack::Static,
:urls => ["/images", "/js", "/css"],
:root => "public"
# Absolute path of the root app
@root = File.expand_path(File.join(File.dirname(__FILE__), 'public'))
run lambda { |env|
req = Rack::Request.new(env)
res = File.join(@root, req.path_info)
index = File.join(res, 'index.html')
ret = 404
if File.directory?(res) and File.exists?(index)
out = File.open(index, File::RDONLY)
ret = 200
elsif File.exists?(res)
out = File.open(res, File::RDONLY)
ret = 200
else
out = File.open('public/404.html', File::RDONLY)
end
[
ret,
{
'Content-Type' => 'text/html',
},
out
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment