Sunday, August 19, 2007

Webrick + Erb = Simple Clean Front End

Use webrick to create a front end for you ruby application. I found embedding ErB (rails like templating) makes ruby ui lightweight and easy.

require 'webrick'
include WEBrick
require 'erb'

s = HTTPServer.new( :Port => 80,:DocumentRoot => Dir::pwd + "/web_docs" )

class SearchServlet < HTTPServlet::AbstractServlet
def do_GET(req, response)
File.open('web_docs/search.rhtml','r') do |f|
@template = ERB.new(f.read)
end
response.body = @template.result(binding)
response['Content-Type'] = "text/html"
end

end
end

s.mount("/search", SearchServlet)

trap("INT"){
s.shutdown
}
s.start

No comments: