Created
March 13, 2009 21:37
-
-
Save rtomayko/78783 to your computer and use it in GitHub Desktop.
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
# http://groups.google.com/group/sinatrarb/browse_thread/thread/84d525759af8615a | |
require 'fileutils' | |
class Staticizer | |
def initialize(app, cache_path) | |
@app = app | |
@cache_path = cache_path | |
end | |
def call(env) | |
status, headers, body = @app.call(env) | |
if status == 200 | |
filename = File.join(@cache_path, env['PATH_INFO']) | |
filename += 'index' if filename =~ /\/$/ | |
filename += '.html' unless filename =~ /\.\w+$/ | |
dirname = File.dirname(filename) | |
FileUtils.mkdir_p dirname | |
File.open(filename, 'wb') do |io| | |
body.each { |part| io.write(part) } | |
end | |
body = File.open(filename, 'rb') | |
end | |
[status, headers, body] | |
end | |
end |
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
require 'rubygems' | |
require 'sinatra' | |
require 'staticizer' | |
use Staticizer, "./static" | |
get '/' do | |
'<html>BAM!</html>' | |
end | |
get '/foo/bar' do | |
"Hello World!" | |
end | |
get '/bling/' do | |
"Oh, nice" | |
end | |
get '/something.xml' do | |
"<foo><bar/></foo>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment