Sha256: 774505b8a5a79007cc91e16202a198794e8ab6f76202406e073f7f629bf02e6f

Contents?: true

Size: 898 Bytes

Versions: 9

Compression:

Stored size: 898 Bytes

Contents

require 'time'

# Reads a favicon.ico statically at load time, renders it on any request for
# '/favicon.ico', and sends every other request on downstream.
#
# Rack::Static is a better option if you're serving several static assets.
#
module Goliath
  module Rack
    class Favicon
      def initialize(app, filename)
        @app = app
        @favicon = File.read(File.join(filename))
        @expires  = Time.at(Time.now + (60 * 60 * 24 * 7)).utc.rfc822.to_s
        @last_modified = File.mtime(filename).utc.rfc822.to_s
      end

      def call(env)
        if env['REQUEST_PATH'] == '/favicon.ico'
          env.logger.info('Serving favicon.ico')

          [200, {'Last-Modified' => @last_modified,
                 'Expires' => @expires,
                 'Content-Type' => "image/vnd.microsoft.icon"}, @favicon]
        else
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
goliath-1.0.7 lib/goliath/rack/favicon.rb
goliath-1.0.6 lib/goliath/rack/favicon.rb
goliath-1.0.5 lib/goliath/rack/favicon.rb
goliath-1.0.4 lib/goliath/rack/favicon.rb
goliath-1.0.3 lib/goliath/rack/favicon.rb
goliath-1.0.2 lib/goliath/rack/favicon.rb
goliath-1.0.1 lib/goliath/rack/favicon.rb
goliath-1.0.0 lib/goliath/rack/favicon.rb
goliath-1.0.0.beta.1 lib/goliath/rack/favicon.rb