Skip to content

Instantly share code, notes, and snippets.

@aqab
Created June 24, 2009 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aqab/135067 to your computer and use it in GitHub Desktop.
Save aqab/135067 to your computer and use it in GitHub Desktop.
# 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