Skip to content

Instantly share code, notes, and snippets.

@bjhess
Created January 26, 2009 17:15
Show Gist options
  • Save bjhess/52868 to your computer and use it in GitHub Desktop.
Save bjhess/52868 to your computer and use it in GitHub Desktop.
require 'lib/caching'
get '/' do
cache(erb(:index, :layout => :main))
end
#! /usr/bin/env ruby
#
# This script is to be run via cron while with the cwd in the
# Sinatra root directory.
#
require 'fileutils'
TWENTY_FOUR_HOURS = 60*60*24
Dir['public/*.html'].each do |path|
mtime = File.mtime(path)
if (mtime.to_i + TWENTY_FOUR_HOURS) < Time.now.to_i
FileUtils.remove_file(path)
end
end
require 'fileutils'
helpers do
def cache(text)
# requests to / should be cached as index.html
uri = request.env["REQUEST_URI"] == "/" ? 'index' : request.env["REQUEST_URI"]
# Don't cache pages with query strings.
unless uri =~ /\?/
uri << '.html'
# put all cached files in public/
path = File.join(File.dirname(__FILE__), '..', 'public', uri)
# Write the text passed to the path
File.open(path, 'w') { |f| f.write( text ) }
end
return text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment