Browse files

Make convenience favicon middleware.

Closes #98.
  • Loading branch information...
1 parent 2c15a37 commit 11288bd2b3aebe295dedd024a2a91b95ef557984 @dj2 dj2 committed Oct 21, 2011
View
40 examples/favicon.rb
@@ -1,40 +0,0 @@
-#!/usr/bin/env ruby
-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.
-#
-# If you will be serving even one more file than this one, you should instead
-# use Rack::Static:
-#
-# use(Rack::Static, # render static files from ./public
-# :root => Goliath::Application.app_path("public"),
-# :urls => ["/favicon.ico", '/stylesheets', '/javascripts', '/images'])
-#
-class Favicon
- def initialize(app, filename)
- @@favicon = File.read(filename)
- @@last_mod = File.mtime(filename).utc.rfc822
- @@expires = Time.at(Time.now + 604800).utc.rfc822 # 1 week from now
- @app = app
- end
-
- def call(env, *args)
- if env['REQUEST_PATH'] == '/favicon.ico'
- return [200, {"Last-Modified"=> @@last_mod.to_s, "Expires" => @@expires, "Content-Type"=>"image/vnd.microsoft.icon"}, @@favicon]
- else
- return @app.call(env)
- end
- end
-end
-
-if File.expand_path($0) == File.expand_path(__FILE__)
- $:<< '../lib' << 'lib'
- require 'goliath'
- puts "starting hello world!"
- class HelloWorld < Goliath::API
- HelloWorld.use(Favicon, File.expand_path(File.dirname(__FILE__)+"/public/favicon.ico"))
- end
- require(File.dirname(__FILE__)+'/hello_world.rb')
-end
View
3 examples/rasterize/rasterize.rb
@@ -2,15 +2,14 @@
$: << File.dirname(__FILE__)+'/../../lib'
require 'goliath'
require 'postrank-uri'
-require File.dirname(__FILE__)+'/../favicon'
# Install phantomjs: http://code.google.com/p/phantomjs/wiki/QuickStart
# $> ruby rasterize.rb -sv
# $> curl http://localhost:9000/?url=http://www.google.com (or rather, visit in the browser!)
class Rasterize < Goliath::API
use Goliath::Rack::Params
- use Favicon, File.expand_path(File.dirname(__FILE__)+"/../public/favicon.ico")
+ use Goliath::Rack::Favicon, File.expand_path(File.dirname(__FILE__)+"/../public/favicon.ico")
use Goliath::Rack::Validation::RequestMethod, %w(GET)
use Goliath::Rack::Validation::RequiredParam, {:key => 'url'}
View
2 examples/websocket.rb
@@ -37,6 +37,8 @@ def response(env)
end
class Websocket < Goliath::API
+ use Goliath::Rack::Favicon, File.expand_path(File.dirname(__FILE__) + '/ws/favicon.ico')
+
get '/', WSInfo
map '/ws', WebsocketEndPoint
end
View
BIN examples/ws/favicon.ico
Binary file not shown.
View
1 lib/goliath/rack.rb
@@ -6,6 +6,7 @@ module Rack
autoload :Builder, 'goliath/rack/builder'
autoload :DefaultMimeType, 'goliath/rack/default_mime_type'
autoload :DefaultResponseFormat, 'goliath/rack/default_response_format'
+ autoload :Favicon, 'goliath/rack/favicon'
autoload :Formatters, 'goliath/rack/formatters'
autoload :Heartbeat, 'goliath/rack/heartbeat'
autoload :JSONP, 'goliath/rack/jsonp'
View
31 lib/goliath/rack/favicon.rb
@@ -0,0 +1,31 @@
+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_mod,
+ 'Expires' => @expires,
+ 'Content-Type' => "image/vnd.microsoft.icon"}, @favicon]
+ else
+ @app.call(env)
+ end
+ end
+ end
+ end
+end

0 comments on commit 11288bd

Please sign in to comment.