Created
February 17, 2015 16:15
-
-
Save leighhalliday/ae30bb219da3d5a7ae87 to your computer and use it in GitHub Desktop.
Rack Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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