Skip to content

Commit 11288bd

Browse files
committed
Make convenience favicon middleware.
Closespostrank-labs#98.
1 parent 2c15a37 commit 11288bd

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

‎examples/favicon.rb‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎examples/rasterize/rasterize.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
$: << File.dirname(__FILE__)+'/../../lib'
33
require'goliath'
44
require'postrank-uri'
5-
requireFile.dirname(__FILE__)+'/../favicon'
65

76
# Install phantomjs: http://code.google.com/p/phantomjs/wiki/QuickStart
87
# $> ruby rasterize.rb -sv
98
# $> curl http://localhost:9000/?url=http://www.google.com (or rather, visit in the browser!)
109

1110
classRasterize < Goliath::API
1211
useGoliath::Rack::Params
13-
useFavicon,File.expand_path(File.dirname(__FILE__)+"/../public/favicon.ico")
12+
useGoliath::Rack::Favicon,File.expand_path(File.dirname(__FILE__)+"/../public/favicon.ico")
1413
useGoliath::Rack::Validation::RequestMethod,%w(GET)
1514
useGoliath::Rack::Validation::RequiredParam,{:key=>'url'}
1615

‎examples/websocket.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def response(env)
3737
end
3838

3939
classWebsocket < Goliath::API
40+
useGoliath::Rack::Favicon,File.expand_path(File.dirname(__FILE__) + '/ws/favicon.ico')
41+
4042
get'/',WSInfo
4143
map'/ws',WebsocketEndPoint
4244
end

‎examples/ws/favicon.ico‎

318 Bytes
Binary file not shown.

‎lib/goliath/rack.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Rack
66
autoload:Builder,'goliath/rack/builder'
77
autoload:DefaultMimeType,'goliath/rack/default_mime_type'
88
autoload:DefaultResponseFormat,'goliath/rack/default_response_format'
9+
autoload:Favicon,'goliath/rack/favicon'
910
autoload:Formatters,'goliath/rack/formatters'
1011
autoload:Heartbeat,'goliath/rack/heartbeat'
1112
autoload:JSONP,'goliath/rack/jsonp'

‎lib/goliath/rack/favicon.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require'time'
2+
3+
# Reads a favicon.ico statically at load time, renders it on any request for
4+
# '/favicon.ico', and sends every other request on downstream.
5+
#
6+
# Rack::Static is a better option if you're serving several static assets.
7+
#
8+
moduleGoliath
9+
moduleRack
10+
classFavicon
11+
definitialize(app,filename)
12+
@app=app
13+
@favicon=File.read(File.join(filename))
14+
@expires=Time.at(Time.now + (60 * 60 * 24 * 7)).utc.rfc822.to_s
15+
@last_modified=File.mtime(filename).utc.rfc822.to_s
16+
end
17+
18+
defcall(env)
19+
ifenv['REQUEST_PATH'] == '/favicon.ico'
20+
env.logger.info('Serving favicon.ico')
21+
22+
[200,{'Last-Modified'=>@last_mod,
23+
'Expires'=>@expires,
24+
'Content-Type'=>"image/vnd.microsoft.icon"},@favicon]
25+
else
26+
@app.call(env)
27+
end
28+
end
29+
end
30+
end
31+
end

0 commit comments

Comments
(0)