Skip to content

Commit

Permalink
Added a favicon interceptor to the examples, and used it in the raste…
Browse files Browse the repository at this point in the history
…rizer examples
  • Loading branch information
Philip (flip) Kromer committed Jul 31, 2011
1 parent 88e6748 commit 6395370
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)
40 changes: 40 additions & 0 deletions examples/favicon.rb
@@ -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
3 changes: 2 additions & 1 deletion examples/http_log.rb
Expand Up @@ -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: {
Expand All @@ -81,4 +82,4 @@ def record(process_time, resp, client_headers, response_headers)
e.mongo.insert(doc)
end
end
end
end
Binary file added examples/public/favicon.ico
Binary file not shown.
3 changes: 2 additions & 1 deletion examples/rasterize/rasterize.rb
Expand Up @@ -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'}

Expand Down

0 comments on commit 6395370

Please sign in to comment.