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)–


In reply to Re: Serving multiple Plack apps by Your Mother
in thread Serving multiple Plack apps by soundX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.