[cgiapp] RFC: first-class support for PSGI in CGI::Application

Mark Stosberg mark at summersault.com
Thu Feb 18 12:58:00 EST 2010

> The final result might look like this:
>     use WebApp;
>     use CGI::PSGI;
>     my $handler = sub {
>         my $env = shift;
>         my $webapp = WebApp->new({ QUERY => CGI::PSGI->new($env) });
>         $webapp->run_as_psgi;
>     };

I've considered this more, I want to submit a second draft of a
proposal. First-class PSGI support should address all of the following

1. Passing in and setting up the PSGI env
2. Returning the PSGI required return format
3. Accessing the environment in an abstracted way. 
4. Wrapping the whole application as a coderef which does steps 1-3.

My initial proposal addressed the the return format, leaving setup and
"env" access to CGI::PSGI, and letting the user deal with 4, the final

Michael Peters rightly pointed that arguments to new({}) is how we make
all the other modifications to what happens in run(), and Miyagawa
suggested that we provide a shortcut to generate the final wrapper as
well, CGI::Application::PSGI does.

So, some proposal refinements:

1. We should add an "env" method as an abstract way to access the
environment. In non-PSGI code, this would just access \%ENV. Using
this, internal code and plugins could access the environment in a way
that would work with our without PSGI. With CGI::PSGI, we could access
this already through $self->query->env, and perhaps $self->env will
just be an alias for that. As a related example, we have this bug
Adding a env() method to CGI::Application would help address it

2. We add a wrapper method which should take the same arguments as new,
but will default to setting up CGI::PSGI correctly and also calling run.
So, running a CGI::Application project on PSGI could be as simple as

WebApp->psgi_app({ ... args to new() ... });

If you want to manually construct the code ref, of course you still

Anyone else have further thoughts on PSGI integration?



 . . . . . . . . . . . . . . . . . . . . . . . . . . . 
   Mark Stosberg            Principal Developer  
   mark at summersault.com     Summersault, LLC     
   765-939-9301 ext 202     database driven websites
 . . . . . http://www.summersault.com/ . . . . . . . .

More information about the cgiapp mailing list