<commit_before>require 'sinatra'

# For Foreman
configure(:development) { STDOUT.sync = true }


# Rack/Cache

require 'rack/cache'
require 'dalli'

use Rack::Cache,
  metastore: Dalli::Client.new,
  entitystore: 'file:tmp/cache/rack/body',
  allow_reload: false

require 'digest/md5'

# Cache unversioned assets for 5 minutes
set :static_cache_control, [:public, max_age: 300]

# Pre-calculate all the asset SHA's
set :assets, Dir["*.css", "**/*.{png,svg,css}"].inject({}) {|acc, file|
  acc[file] = Digest::MD5.file(file).hexdigest
  acc
}

helpers do
  def asset_path(asset)
    sha = settings.assets["public/#{asset}"]
    asset.sub(/\.(.*?)$/, ".#{sha}." + '\1')
  end
end

get '/' do
  cache_control :public
  erb(:"index.html", layout:false).tap do |html|
    etag Digest::MD5.hexdigest(html)
  end
end

# Serve up the sha'd asset URLs, regardless of the actual sha value
settings.assets.each_pair do |path, sha|
  p = Pathname.new(path)
  get Regexp.new('/' + Regexp.escape(p.to_s).sub(/(\\\..*?)$/, '\\\.' + sha + '\1').sub('public/','')) do
    expires 60*60*24*365, :public
    content_type Rack::Mime.mime_type(File.extname(path))
    etag sha
    send_file path
  end
end
<commit_msg>More Rack::Cache tweakage<commit_after>require 'sinatra'

# For Foreman
configure(:development) { STDOUT.sync = true }


# Rack/Cache

require 'rack/cache'
require 'dalli'

use Rack::Cache,
  metastore: Dalli::Client.new,
  entitystore: 'file:tmp/cache/rack/body',
  allow_reload: false,
  default_ttl: 60

require 'digest/md5'

set :static_cache_control, [:public]

# Pre-calculate all the asset SHA's
set :assets, Dir["*.css", "**/*.{png,svg,css}"].inject({}) {|acc, file|
  acc[file] = Digest::MD5.file(file).hexdigest
  acc
}

helpers do
  def asset_path(asset)
    sha = settings.assets["public/#{asset}"]
    asset.sub(/\.(.*?)$/, ".#{sha}." + '\1')
  end
end

get '/' do
  erb(:"index.html", layout:false).tap do |html|
    etag Digest::MD5.hexdigest(html)
    cache_control :public
  end
end

# Serve up the sha'd asset URLs, regardless of the actual sha value
settings.assets.each_pair do |path, sha|
  p = Pathname.new(path)
  get Regexp.new('/' + Regexp.escape(p.to_s).sub(/(\\\..*?)$/, '\\\.' + sha + '\1').sub('public/','')) do
    etag sha
    expires 60*60*24*365, :public
    content_type Rack::Mime.mime_type(File.extname(path))
    send_file path
  end
end

{'fraction_non_alphanumeric': 0.18507588532883643, 'fraction_numerical': 0.01433389544688027, 'mean_word_length': 3.902892561983471, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3147825', 'n_tokens_mistral': 957, 'n_tokens_neox': 909, 'n_words': 206}