Skip to content

Instantly share code, notes, and snippets.

@jch
Created April 30, 2012 05:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jch/2555714 to your computer and use it in GitHub Desktop.
Save jch/2555714 to your computer and use it in GitHub Desktop.
Programmatically start a rack app
require 'rack'
class RackApp
def self.call(env)
[200, {'Content-Type' => 'text/html'}, ['derp']]
end
end
# For a full list of options, see
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/webrick/rdoc/WEBrick.html
options = {
:Host => '127.0.0.1',
:Port => '3000'
}
Rack::Handler::WEBrick.run(RackApp, options) do |server|
[:INT, :TERM].each { |sig| trap(sig) { server.stop } }
end
@radfahrer
Copy link

Thank you @jch! It's taken me quite a bit of searching to find this gem of an example. You have made my day. I do wonder why you chose WEBrick?

@sheldonh
Copy link

sheldonh commented Feb 5, 2018

@radfahrer WEBrick is part of the Ruby standard library, so it's always available. That's nice for test suites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment