diff --git a/README.md b/README.md index 685ef56f..0992c2b9 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/examples/favicon.rb b/examples/favicon.rb new file mode 100755 index 00000000..051e88c3 --- /dev/null +++ b/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 diff --git a/examples/http_log.rb b/examples/http_log.rb index 21e1aa65..1fa4d858 100755 --- a/examples/http_log.rb +++ b/examples/http_log.rb @@ -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 \ No newline at end of file +end diff --git a/examples/public/favicon.ico b/examples/public/favicon.ico new file mode 100644 index 00000000..7895fba2 Binary files /dev/null and b/examples/public/favicon.ico differ diff --git a/examples/rasterize/rasterize.rb b/examples/rasterize/rasterize.rb index a18c5e1c..d0b4e4d1 100755 --- a/examples/rasterize/rasterize.rb +++ b/examples/rasterize/rasterize.rb @@ -2,6 +2,7 @@ $: << 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 @@ -9,7 +10,7 @@ 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'}