Software > Ruby > Ruby web programming >
(on Wikipedia)
https://github.com/ruby/webrick [ 1 ] was http://www.webrick.org/
A Ruby library program to build HTTP servers.
Mongrel is reportedly faster/cooler.
Table of Contents [hide]
Usage ∞
‘hello world’ ∞
Spoiler
#!/usr/bin/ruby require 'webrick' include WEBrick s = HTTPServer.new(:Port => 2000) class HelloServlet < HTTPServlet::AbstractServlet def do_GET(req,res) res['Content-Type'] = "text/html" res.body = %{ <html><body> <p>Hello. #{req['User-Agent']}</p> <p>Parameters: #{req.query.keys.join(', ')}</p> </body></html> } end # def do_GET(req,res) end # class Hello < HTTPServlet::ABstractServlet s.mount("/", HelloServlet) trap("INT"){s.shutdown} s.start
Another example ∞
Spoiler
require 'webrick' class Configureable < Simple def initialize(server, color, size) super(server) @color = color @size = size end def do_stuff_with(request) content = "<p style=\"color: #{@color}; font-size: #{@size}\">some text" return 200, "text/html", content end end class Simple < WEBrick::HTTPServlet::AbstractServlet def do_stuff_with(request) return 200, "text/plain", "you got a page" end def do_GET(request, response) status, content_type, body = do_stuff_with(request) response.status = status response['Content-Type'] = content_type response.body = body end end if $0 == __FILE__ then server = WEBrick::HTTPServer.new(:Port => 8000) # http://localhost:8000/ server.mount "/", Simple # http://localhost:8000/subdir server.mount "/subdir", Configureable, "red", "2em" trap "INT" do server.shutdown end server.start end
Notes ∞
- Gnome’s Guide to WEBrick
- WEBrick articles
- Basic Auth in WEBrick
- At the Forge – Getting Started with Ruby
- Dynamic WEBRick Servers in Ruby
Footnotes

ported