Sha256: c2d3cadb991c8d30371baa38673f772992a19970bc6708d2ed5e0d4e5e795bbb

Contents?: true

Size: 858 Bytes

Versions: 4

Compression:

Stored size: 858 Bytes

Contents

module Locomotive
  module Middlewares
    class Fonts

      def initialize(app, opts = {})
        @app = app
        @path_regexp = opts[:path] || %r{^/fonts/}
        @expires_in = opts[:expires_in] || 24.hour # varnish
      end

      def call(env)
        if env["PATH_INFO"] =~ @path_regexp
          site = fetch_site(env['SERVER_NAME'])

          if site.nil?
            @app.call(env)
          else
            body = ThemeAssetUploader.build(site, env["PATH_INFO"]).read.to_s

            [200, { 'Cache-Control' => "public; max-age=#{@expires_in}" }, [body]]
          end
        else
          @app.call(env)
        end
      end

      protected

      def fetch_site(domain_name)
        Rails.cache.fetch(domain_name, :expires_in => @expires_in) do
          Site.match_domain(domain_name).first
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotive_cms-1.0.0.beta.2 lib/locomotive/middlewares/fonts.rb
locomotive_cms-1.0.0.beta lib/locomotive/middlewares/fonts.rb
locomotive_cms-0.0.4.beta12 lib/locomotive/middlewares/fonts.rb
locomotive_cms-0.0.4.beta11 lib/locomotive/middlewares/fonts.rb