Skip to content

Instantly share code, notes, and snippets.

@leighhalliday
Created February 17, 2015 16:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leighhalliday/ae30bb219da3d5a7ae87 to your computer and use it in GitHub Desktop.
Save leighhalliday/ae30bb219da3d5a7ae87 to your computer and use it in GitHub Desktop.
Rack Example
# Use:
# This is in file config.ru
# 1) gem install rack
# 2) rackup
# 3) visit http://localhost:9292 in browser
# 4) Timer info will be in console
# 5) when changing code, will have to restart rackup
# Include rack
require 'rack'
# Our App
class HelloApp
def self.call(env)
[200, {'Content-Type' => 'text/html'}, ["Hello there."]]
end
end
# Timer middlewhere
class Timer
def initialize(app)
@app = app
end
def call(env)
before = Time.now.to_f
response = @app.call env
puts "#{(Time.now.to_f - before) * 1000.0} milliseconds."
response
end
end
# Using Rack Builder to set up App and Middleware
app = Rack::Builder.new do
use Timer
run HelloApp
end
# Run the app
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment