Multiple executables and apps with one PSGI server
Proof of concept to help you and others (and me the next time I want to stub out an example). Tested to work on my OS X box. I recommend uWSGI as an app server and nginx as a webserver (it doesn’t like CGI so you might have to forgo it unless you get ALL your CGIs ironed out to PSGI).
Setup…
Install or skip/edit the parts that use: strictures, Catalyst, Catalyst::Devel, Mojolicious, Plack, Plack::Builder, Plack::Middleware::Rewrite… maybe some I missed. Judicious middleware use can let you do all kinds of unified logging or even sessions (x-app you’d need to unify the handling and name but not as hard as from scratch).
Update: DERP indeed, left these out, Plack::App::CGIBin, Plack::App::WrapCGI.
Make a play dir and get in it (update, added the cgi “bin”)–
cd
mkdir pm-1116108
cd pm-1116108
mkdir cgi
Create Cat app–
catalyst.pl MyCatalystApp
Create hello.php–
<?php print "Hello, World!"; ?>
Create cgi/hello.cgi (need the “bin”)–
#!/usr/bin/env perl
use strictures;
use CGI qw(:standard);
print header(),
start_html("HAI"),
h1("DERP!"),
end_html();
Create mojo-hello.pl–
use Mojolicious::Lite;
get '/' => sub {
my $c = shift;
$c->render(text => "OHAI!");
};
app->start;
Create app.psgi–
use strictures;
use utf8;
use Plack::Builder;
use Encode;
require Mojo::Server::PSGI;
require Plack::App::CGIBin;
require Plack::App::WrapCGI;
use lib "./MyCatalystApp/lib";
require MyCatalystApp;
my $root = sub {
[ 200,
[ "Content-Type" => "text/plain; charset=utf-8" ],
[ encode_utf8("I \x{2763} Plack::Builder") ] ];
};
my $cat_app = MyCatalystApp->psgi_app;
my $mojo_app = eval {
my $server = Mojo::Server::PSGI->new;
$server->load_app("./mojo-hello.pl");
$server->to_psgi_app;
};
my $cgi_app = Plack::App::CGIBin
->new( root => "./cgi" )
->to_app;
my $php = Plack::App::WrapCGI
->new( script => "/usr/bin/php ./hello.php",
execute => 1 )
->to_app;
builder {
enable "Rewrite", rules => sub {
s,(?<=/cat)\z,/,; # Add trailing slash for Cat app’s root.
};
mount "/cat" => $cat_app;
mount "/mojo" => $mojo_app;
mount "/cgi" => $cgi_app;
mount "/php" => $php;
mount "/" => $root;
};
__END__
Start it up (with restart flag to watch for edits, Cat does lots of debug for a new app so we turn it off here)–
env CATALYST_DEBUG=0 plackup -r
Watching ./lib pm-1116108.psgi for file updates.
HTTP::Server::PSGI: Accepting connections at http://0:5000/
Now visit (might need to change “localhost” depending on your box’s setup)–
- http://localhost:5000/
- http://localhost:5000/cat
- http://localhost:5000/mojo
- http://localhost:5000/cgi/hello.cgi
- http://localhost:5000/php
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.