Permalink
Browse files

Added a favicon interceptor to the examples, and used it in the raste…

…rizer examples
  • Loading branch information...
1 parent 88e6748 commit 63953707f339eb5961d8d063902233afd533d0a0 Philip (flip) Kromer committed Jul 31, 2011
Showing with 45 additions and 2 deletions.
  1. +1 −0 README.md
  2. +40 −0 examples/favicon.rb
  3. +2 −1 examples/http_log.rb
  4. BIN examples/public/favicon.ico
  5. +2 −1 examples/rasterize/rasterize.rb
View
@@ -93,3 +93,4 @@ Goliath has been in production at PostRank for over a year, serving a sustained
## License & Acknowledgments
Goliath is distributed under the MIT license, for full details please see the LICENSE file.
+Rock favicon CC-BY from [Douglas Feer](http://www.favicon.cc/?action=icon&file_id=375421)
View
@@ -0,0 +1,40 @@
+#!/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
@@ -56,6 +56,7 @@ def to_http_header(k)
# Write the request information into mongo
def record(process_time, resp, client_headers, response_headers)
e = env
+ e.trace('http_log_record')
EM.next_tick do
doc = {
request: {
@@ -81,4 +82,4 @@ def record(process_time, resp, client_headers, response_headers)
e.mongo.insert(doc)
end
end
-end
+end
View
Binary file not shown.
@@ -2,14 +2,15 @@
$: << 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::Validation::RequestMethod, %w(GET)
use Goliath::Rack::Validation::RequiredParam, {:key => 'url'}

0 comments on commit 6395370

Please sign in to comment.