Simon Rozet
2008-Jan-04 08:23 UTC
[Mongrel] Howto write a mongrel handler for a CGI app using CGIWrapper
Hello,
Just for further reference in case of someone else want to do the same :
I wanted to write a mongrel for a CGI app :
[[[
require ''cgi''
require ''foo''
cgi = CGI.new
if !cgi[''uri''] || (cgi[''uri''] ==
'''')
Foo.error "URI argument is required"
end
uri = cgi[''uri'']
user = cgi[''username'']
pass = cgi[''password'']
foo = Foo.new(:output => ''html'')
if user == ''''
foo.check(uri)
else
foo.check(uri, user, pass)
end
foo.report
]]]
Here is the mongrel version, using Mongrel::CGIWrapper :
[[[
require ''mongrel''
require ''cgi''
require ''foo''
class AppHandler < Mongrel::HttpHandler
def process(request, response)
cgi = Mongrel::CGIWrapper.new(request, response)
if !cgi[''uri''] || (cgi[''uri''] ==
'''')
response.start(200, true) do |header, body|
Foo.error("URI argument is required", output=body)
end
end
format = request.params[''HTTP_ACCEPT''] ==
''text/plain'' ? ''text'' :
''html''
ape = Ape.new({ :crumbs => true, :output => format })
if cgi[''user''] && cgi[''pass'']
ape.check(cgi[''uri''], cgi[''user''],
cgi[''pass''])
else
ape.check(cgi[''uri''])
end
response.start(200, true) do |head, body|
ape.report(output=body)
end
end
end
h = Mongrel::HttpServer.new(''0.0.0.0'', 4000)
h.register(''/'',
Mongrel::RedirectHandler.new(''/ape/index.html''))
h.register(''/ape'',
Mongrel::DirHandler.new(File.dirname(__FILE__) +
''/layout'', true))
h.register(''/atompub/go'', ApeHandler.new)
h.run.join
]]]
--
Simon Rozet <simon at rozet.name>
Simon Rozet
2008-Jan-04 08:31 UTC
[Mongrel] Howto write a mongrel handler for a CGI app using CGIWrapper
On 1/4/08, Simon Rozet <simon at rozet.name> wrote:> Hello, > > Just for further reference in case of someone else want to do the same :Err, sorry. I unwittingly hit <tab> <return> ... so here is the correct message :> I wanted to write a mongrel handler for a CGI app : > [[[ > require ''cgi'' > require ''foo'' > > cgi = CGI.new > > if !cgi[''uri''] || (cgi[''uri''] == '''') > Foo.error "URI argument is required" > end > > uri = cgi[''uri''] > user = cgi[''username''] > pass = cgi[''password''] > > foo = Foo.new(:output => ''html'') > > if user == '''' > foo.check(uri) > else > foo.check(uri, user, pass) > end > foo.report > ]]]Here is the mongrel version, using Mongrel::CGIWrapper : [[[ require ''mongrel'' require ''cgi'' require ''foo'' class FooHandler < Mongrel::HttpHandler def process(request, response) cgi = Mongrel::CGIWrapper.new(request, response) if !cgi[''uri''] || (cgi[''uri''] == '''') response.start(200, true) do |header, body| # Foo.error accept an IO object to write to Foo.error("URI argument is required", output=body) end end format = request.params[''HTTP_ACCEPT''] == ''text/plain'' ? ''text'' : ''html'' ape = Foo.new(:output => format) if cgi[''user''] && cgi[''pass''] Foo.check(cgi[''uri''], cgi[''user''], cgi[''pass'']) else Foo.check(cgi[''uri'']) end response.start(200, true) do |head, body| Foo.report(output=body) end end end h = Mongrel::HttpServer.new(''0.0.0.0'', 5000) h.register(''/foo'', FooHandler.new) h.run.join ]]] That''s it. Now I can launch the app from the command line without the pain of any server configuration. Thanks to Evan for pointing me to the right direction btw -- Simon Rozet <simon at rozet.name>