public
Last active

  • Download Gist
rack_middleware_example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.