Index: [Article Count Order] [Thread]

Date: Mon, 13 Jun 2005 00:53:11 +0300
From: TAKANO Noriyuki <noriyuki.takano@tut.fi>
Subject: [webricken:148] Re: regarding ERBHandler
To: webricken@notwork.org
Message-Id: <42ACAEC7.60201@tut.fi>
X-Mail-Count: 00148

Dear Jho,

I don't know much about ERBHandler because I am using it in the easiest 
way most often.
That is, WEBrick::HTTPServer in default automatically runs erb 
(ERBHandler) when
the requested file is *.rhtml.  (this is what you might have missed.)
A large help by ERBHandler is that inside erb file the query is referred 
to as "query" variable.
(this might have beeen too.)
One thing we have to be careful for is that we had better clarify the 
MIME type for *.rhtml files.

I wish this posting could help you.
I might assume the simple thread below is just enough to be an example 
for your case.

### ex1.rb
  # sample thread and global
  $counter = {"value" => 0, "run" => false}
  count = Thread.start{
    loop{
      $counter["value"] += 1 if $counter["run"]
      sleep(5)
    }
  }
 
  doc_root = "#{Dir::pwd}/htdocs" # for example of document directory
  conf = {:Port => 8080, :DocumentRoot => doc_root}
  server = HTTPServer.new(conf)

  # maybe better to add this MIME
  server.config[:MimeTypes]["rhtml"] = "text/html"
 
  trap("INT"){ server.shutdown }
  server.start
  Thread.kill(count)
### end of ex1.rb

I think counter.rhtml I put here works just fine if you copy it to 
htdocs directory under
the working directory of ex1.rb,
.
<!-- counter.rhtml -->
<%
  # ERBHandler let us see the query in the erb template
  if query["start"] == "on" then $counter["run"] = true
  elsif query["pause"] == "on" then $counter["run"] = false
  end
%>
<HTML>
  <HEAD><TITLE>Counter Demo</TITLE></HEAD>
  <BODY>
  Counter value <%= $counter["value"] %>, currently <%= $counter["run"] 
? "running" : "paused" %>
  <FORM action='counter.rhtml'>
    <% if $counter["run"] %>
    <INPUT type='checkbox' name='pause'> pause
    <% else %>
    <INPUT type='checkbox' name='start'> start
    <% end %>
    <INPUT type='submit' value='send this command'>
  </FORM>
  </BODY>
</HTML>
<!-- end of counter.rhtml -->

If really need to touch ERBHnadler, well... I might do like this

### ex2.rb
class Counter < HTTPServlet::ERBHandler
  # nothing special, only having erb file fixed
  def initialize(server, name = "#{Dir::pwd}/htdocs/counter.txt")
    super
  end
end

if $0 == __FILE__
  # this if-block runs only when ex2.rb is executed (not required nor 
loaded)
 
  # sample thread and global
  $counter = {"value" => 0, "run" => false}
  count = Thread.start{
    loop{
      $counter["value"] += 1 if $counter["run"]
      sleep(5)
    }
  }
 
  doc_root = "#{Dir::pwd}/htdocs" # for example of document directory
  conf = {:Port => 8080, :DocumentRoot => doc_root}
  server = HTTPServer.new(conf)

  # maybe better to add this MIME
  server.config[:MimeTypes]["rhtml"] = "text/html"

  # mount the servlet derived from ERBHandler
  server.mount("/counter", Counter)

  # for shut down by browser
  server.mount_proc("/end"){|req, res| server.shutdown}
 
  trap("INT"){ server.shutdown }
  server.start
  Thread.kill(count)
end

### end of ex2.rb

ex2.rb uses Counter servlet, which inherits ERBHandler to convert
counter.txt in the htdocs directory, in addition to serving counter.rhtml
(http://hostname:8080/counter works, not http://hostname:8080/counter.txt)

(counter.txt)
  Counter value <%= $counter["value"] %>,
  currently <%= $counter["run"] ? "running" : "paused" %>
(end of counter.txt)

There can be much better ways.
I hope you enjoy Ruby and WEBrick

Nori