[dancer-users] Multiple apps with Plack and Apache

Amelia Ireland aireland at lbl.gov
Wed Jul 13 00:07:54 BST 2016


It's probably easiest to have Apache serve the images, js, and css
directly; put them all in central resources directory that Apache can
serve. Here's a snippet from my httpd.conf file:

RewriteEngine On

# Dancer static file redirect
RewriteRule ^/(css|images|js)/(.*) /web/public/$1/$2 [L]
RewriteRule ^/(.*).html /web/public/$1.html [L]

# Plack server
# twiggy
ProxyPass /async http://localhost:4010/
ProxyPassReverse /async http://localhost:4010/
# starman
ProxyPass / http://localhost:5678/
ProxyPassReverse / http://localhost:5678/

All my static files (css, images, javascript, plus static html pages) are
in the directory /web/public, and the first two rewrite rules parse
incoming requests and serve those files directly. This filters out all URLs
starting with /css/... /js/... /images/... and URLs ending with '.html'.

I have two Dancer-based apps running, using Starman and Twiggy as the
servers, and proxied to different ports. Everything under /async is handled
by Twiggy, and any requests that have not been caught by any of the
previous filters are served by Starman.

The Dancer apps are launched by running "plackup -s Starman --workers=10
bin/app.psgi" or "plackup -s Twiggy bin/async-app.psgi".

You can serve CGI content using Apache (by setting up a ScriptAliased
directory) or Plack; to use Plack, you can add something like this to your
app.psgi file:

use Plack::Builder;

use AwesomeDancerApp;
my $new_app = sub {
AwesomeDancerApp->to_app;
};

use Plack::App::CGIBin;
my $old_cgi = sub {
Plack::App::CGIBin->new(root => "/old_code/" )->to_app;  # this creates
apps for all the CGIs in the directory 'old_code'
};

builder {
mount "/cgi-bin" => $old_cgi->();
mount "/" => $pp->();
};

I hope that is helpful!


On 12 July 2016 at 14:07, janet davis <oscarjwoofer at gmail.com> wrote:

> Thank you for the quick response.
>
> I think I've had a lightbulb moment, proxying through Apache works. The
> only problem is the uri_base doesn't point to the public directory in my
> application root it points to / so no css/js etc.
>
> I have:
> ProxyPass /app1 http://localhost:5000
> ProxyPassReverse /app1 http://localhost:5000
> And I can reach app1 on myserver.com/app1
>
> Is there anywhere in the config that I can specify the correct uri_base
> per app?
>
> I will try using Plack Builder with some example apps.  Are there any
> performance implications if I run a lot of Dancer apps in this way (15+)
> alongside my cgi scripts?  I don't want to go down this route and then
> discover that I have issues with server resources.
>
> Thanks again J.
>
> On Tue, Jul 12, 2016 at 9:21 PM, Chad Wallace <cwallace at lodgingcompany.com
> > wrote:
>
>> On Tue, 12 Jul 2016 19:55:31 +0100
>> janet davis <oscarjwoofer at gmail.com> wrote:
>>
>> > Hi there,
>> >
>> > I have a web server running Apache with a single vhost that serves a
>> > lot of Perl cgi scripts from cgi-bin.
>> >
>> > I've been trying out Dancer, which I really like, but I can't figure
>> > out the best way to deploy apps in my Apache environment.  I want to
>> > keep my Apache cgi configuration to serve legacy scripts but I want
>> > to start building new projects based on psgi in something like Dancer.
>> >
>> > I want Apache to use www.myserver/cgi-bin/appname/script.pl for old
>> > code (which currently works) and www.myserver/app1 for newer Dancer
>> > based apps. Please don't think I'm being lazy by asking, I have read
>> > the documentation and looked at the example for deploying multiple
>> > apps via fcgi but I am still confused as I have limited experience
>> > with Apache - I am working on that.  What's the best way to set this
>> > up, bearing in mind that I may want to run say 10-15 Dancer apps from
>> > my server alongside my cgi-scripts? Do I have to set up a vhost for
>> > each app?  I have Apache::Plack::Handler but I'm not sure whether I
>> > should be using Plack::Builder to mount my apps (which is mentioned
>> > in an older Dancer Cookbook, but not the current deployment guide.
>> > Can someone help me get on the right track please?
>>
>> I'm not sure it matches what you currently have in mind, but what I
>> would do is set up www.myserver/app1 with mod_proxy to a standalone
>> Plack/Dancer process for the app.  You would need something like this
>> in your VirtualHost for each app:
>>
>>     ProxyPass  /app1  http://localhost:5000/app1
>>
>> You'd also have to manage starting, stopping and reloading each of your
>> apps on its own, with a dedicated port, like 5000 in the line above.
>>
>> If you wanted to combine your apps into one, you can do that with
>> Plack::Builder in a single app.psgi script.  Then you get the benefit
>> of only having one script to start and stop, and one port.  I currently
>> do that with one of my apps to separate the AJAX-specific stuff, based
>> on code I found in Dancer2::Cookbook[1]:
>>
>> #!/usr/bin/env perl
>>
>> use strict;
>> use warnings;
>>
>> use FindBin;
>> use lib "$FindBin::Bin/../lib";
>>
>> use Plack::Builder;
>> use newtlc;
>> use newtlc::API;
>>
>> builder {
>>     mount '/'    => newtlc->to_app;
>>     mount '/api' => newtlc::API->to_app;
>> };
>>
>>
>> [1]
>>
>> https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Cookbook.pod#App-specific-feature
>>
>>
>> --
>>
>> C. Chad Wallace, B.Sc.
>> The Lodging Company
>> http://www.lodgingcompany.com/
>> OpenPGP Public Key ID: 0x262208A0
>>
>> _______________________________________________
>> dancer-users mailing list
>> dancer-users at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>>
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20160712/161a2e13/attachment-0001.html>


More information about the dancer-users mailing list