On 1/4/08, Simon Rozet <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]>
_______________________________________________
Mongrel-users mailing list
Mongrel-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-users

Reply via email to