Sizuku.rb の WEBrick 対応
2005-07-22
なかなか時間がとれない今日この頃。 かんさんから「SizukuとWEBrickは相性がいい」とコメントを頂いたので、lily2::managerを参考にしながら WEBrick 対応の方法を考えてみた。 こんなにシンプルには実現できないだろうけど、この方向で組み込みを考えてみるかな。
#!/usr/bin/env ruby
require 'webrick'
require 'webrick/cgi'
module Sizuku
module Handle
def do_GET(req, res)
handle_main(req, res)
end
def do_POST(req, res)
handle_main(req, res)
end
end
class AppliFactory
def AppliFactory.make(c, option = nil)
c.extend(Sizuku::Handle)
$0 == __FILE__ ? make_webrick(c, option) : make_cgi(c, option)
end
def AppliFactory.make_cgi(c, option)
app = WEBrick::CGI.new
app.extend(c)
app
end
def AppliFactory.make_webrick(c, option)
servlet = WEBrick::HTTPServlet::AbstractServlet
servlet.extend c
app = WEBrick::HTTPServer.new(option)
app.mount('/', servlet)
trap("INT"){ app.shutdown }
app
end
end
end
module MyApp
def handle_main(req, res)
res.body = 'hello'
end
end
option = {:Port => 8080}
app = Sizuku::AppliFactory.make(MyApp, option)
app.start