Created
June 24, 2009 07:08
-
-
Save aqab/135067 to your computer and use it in GitHub Desktop.
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
# Rack middleware example | |
require 'rubygems' | |
require 'rack' | |
class MyRackMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
request = Rack::Request.new(env) | |
if request.path =~ /params/ | |
[200,{"Content-Type" => "text/html"}, "#{request.params.inspect}"] | |
else | |
@app.call(env) | |
end | |
end | |
end | |
if $0 == __FILE__ | |
require 'rack/contrib' | |
log_file = File.open('rack_app.log',"a") | |
app = Rack::Builder.app { | |
use Rack::CommonLogger, log_file | |
use Rack::ShowExceptions | |
use Rack::Lint | |
use MyRackMiddleware | |
run Rack::NotFound.new('404.html') | |
} | |
begin | |
Rack::Handler::WEBrick.run app, :Port => 9292 | |
ensure | |
log_file.close | |
end | |
end | |
__END__ | |
To run as a Rack application: | |
$> ruby rack_app.rb | |
If you do not have the rack-contrib gem, install by: | |
$> gem install rack-rack-contrib --source=http://gems.github.com/ | |
See the output by typing the following into the browser | |
http://localhost:9292/params?hello=world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment