My mindset comes from PHP/JSP-type languages where you embed code into
HTML. It seems the way to do this with ruby is to use Apache + mod_ruby
- eruby. It also seems this is not the preferred way to do things. (Right?)
its not the ‘Rails’ way of doing things[1], but its a way that will be
alive as long as the web. even new projects, such as IBM’s Project Zero
“focused on the agile development of the next generation of dynamic Web
applications” take this approach with a directory containing more or
less one PHP file for each resource class, organized by HTTP path…
[1] the Rails way is to use synthetic URLs, and Ruby code to map those
to ActiveRecord objects, which reside in SQL tables. Merb, Iowa also
share similar paradigm…
you can adjust the GET method below to do something like:
path = PathName.new(’./resources/’+r.params[“REQUEST_PATH”])
(path.exist && path.extname.match(/.mab$/) && MarkabyHandler ||
Mongrel::DirHandler).new(Path).process(r,re)
MarkabyHandler is an exercise to you. i’d do (Templates[path] ||=
eval(“lambda{”+path.read+"}")).call or similar to reuse (memoize) the
parsed file…
of course you could make a controllers/ dir to match resources (renaming
that to views), consolidating both to only have a file per
controller/view per resource class [2], etc… Mongrel lets you build up
exactly what you need, instead of what framework authors think you need.
[2] Mongrel facilitates this due to a feature of its URIClassifier.
‘/trinkets/3’ will actually match a ‘/trinkets’ registry. afaik you can
even get the ‘excess’ portion of the match so you dont need to do regex
of your own (it returns 3 results on a lookup). forget the details as
its been a couple months since i dug into the URIClassifier code.
So: How can I get a simple hello world webpage running from Mongrel,
without any framework?
setup a configurator line and a class to match, eg:
#!/usr/bin/env ruby
Path=File.dirname FILE
%w{rubygems markaby mongrel}.each{|r|require r}
class Resource < Mongrel::HttpHandler
def GET r,e
e.start{|h,o| o.write “hello world”}
end
def process r,e
send r.params[“REQUEST_METHOD”], r, e
end
end
if FILE== $0
Mongrel::Configurator.new :host => (ARGV[0] || “0.0.0.0”) do
listener :port => (ARGV[1] || 80) do
uri ‘/’, :handler => Resource.new
run
end
end.join
end
cheers, and welcome to Mongrel land - where all the animals are ugly