2010-10-11 14:26:46 +00:00
|
|
|
module Locomotive
|
|
|
|
module Middlewares
|
|
|
|
class Fonts
|
|
|
|
|
|
|
|
def initialize(app, opts = {})
|
|
|
|
@app = app
|
|
|
|
@path_regexp = opts[:path] || %r{^/fonts/}
|
2011-01-03 20:53:33 +00:00
|
|
|
@expires_in = opts[:expires_in] || 24.hour # varnish
|
2010-10-11 14:26:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
if env["PATH_INFO"] =~ @path_regexp
|
|
|
|
site = fetch_site(env['SERVER_NAME'])
|
|
|
|
|
|
|
|
if site.nil?
|
|
|
|
@app.call(env)
|
|
|
|
else
|
2011-01-26 13:07:33 +00:00
|
|
|
body = ThemeAssetUploader.build(site, env['PATH_INFO']).read.to_s
|
2010-10-11 14:26:46 +00:00
|
|
|
|
2011-01-03 20:53:33 +00:00
|
|
|
[200, { 'Cache-Control' => "public; max-age=#{@expires_in}" }, [body]]
|
2010-10-11 14:26:46 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def fetch_site(domain_name)
|
2011-01-26 13:07:33 +00:00
|
|
|
site_id = Rails.cache.fetch(domain_name, :expires_in => @expires_in) do
|
|
|
|
Site.match_domain(domain_name).only(:id).first._id.to_s rescue ''
|
2010-10-11 14:26:46 +00:00
|
|
|
end
|
2011-01-26 13:07:33 +00:00
|
|
|
|
|
|
|
site_id.blank? ? nil : Site.new(:id => site_id)
|
2010-10-11 14:26:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|