Module: Clayoven::Httpd

Defined in:
lib/clayoven/httpd.rb

Class Method Summary collapse

Class Method Details

.startObject



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
# File 'lib/clayoven/httpd.rb', line 5

def self.start
  port = 8000
  callback = Proc.new do |req, res|

    # A couple of URL rewriting rules.  Not real URL rewriting
    # like .htaccess; just a HTTP redirect. / is rewritten to
    # index.html, and anything-without-a-period is rewritten to
    # that-thing.html.
    if %r{^/$} =~ req.path_info
      res.set_redirect WEBrick::HTTPStatus::Found, "index.html"
    end
    if %r{^/([^.]*)$} =~ req.path_info
      res.set_redirect WEBrick::HTTPStatus::Found, "#{$1}.html"
    end
  end

  server = WEBrick::HTTPServer.new(:Port            => port,
                                   :RequestCallback => callback,
                                   :DocumentRoot    => Dir.pwd)

  puts "clayoven serving at: http://localhost:#{port}"

  trap(:INT) { server.shutdown }
  server.start
end