Sha256: 02a11f88ba578b68dce6dc1337c7a224ca12c122f5fbb5030a47d46eb0090d19

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Pakyow
  module Assets
    class Middleware
      def initialize(app)
        @app = app
      end

      def call(env)
        if Pakyow::Config.assets.compile_on_request
          path = Pakyow::Assets.compiled_asset_path_for_request_path(env['PATH_INFO'])
        else
          path = File.join(Pakyow::Config.assets.compiled_asset_path, env['PATH_INFO'])
        end

        if path =~ /\.(.*)$/ && File.exists?(path)
          catch :halt do
            app = Pakyow.app.dup
            app.context = AppContext.new(Request.new(env), Response.new)

            headers = {
              'Content-Type' => Rack::Mime.mime_type(File.extname(path))
            }

            if Pakyow::Config.assets.cache
              mtime = File.mtime(path)
              headers['Age'] = (Time.now - mtime).to_i
              headers['Cache-Control'] = 'public, max-age=31536000'
            end

            [200, headers, File.open(path)]
          end
        else
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pakyow-assets-0.1.1 lib/middleware.rb
pakyow-assets-0.1.0 lib/middleware.rb