The Plack::Builder allows mount multiple hosts, e.g. something as the following snippet:
my @sites = load_site_names();
my $apps;
for my $site (@sites) {
$apps->{$site} = Some::PsgiFramework::MyApp->new( config => get_config($site) );
}
use Plack::Builder;
builder {
for my $site (@sites) {
mount "$site" => $apps->{$site};
}
mount '/' => sub { ... };
}
e.g.
- the
load_site_namesreturns a list of sites likehttp://example.com,http://some.other.site.com, ... - every "virtual-host" will use the same
Some::PsgiFramework::MyApp - just their config is different
I need exactly the above - need develop one simple web-app which should be deployed for hunderts of different (low-traffic) sites and don't want setup an different PSGI server for each site.
However, the author of the Plack itself says (in the Plack::Request)
Note that this module is intended to be used by Plack middleware developers and web application framework developers rather than application developers (end users).
Writing your web application directly using Plack::Request is certainly possible but not recommended: it's like doing so with mod_perl's Apache::Request: yet too low level.
If you're writing a web application, not a framework, then you're encouraged to use one of the web application frameworks that support PSGI (http://plackperl.org/#frameworks), or see modules like HTTP::Engine to provide higher level Request and Response API on top of PSGI.
And this is the problem.
I checked many of different PSGI based frameworks in the MetaCPAN. And AFAIK each is singleton based, e.g. doesn't allows write applications which could be shared (mounted) many times for different sites in the same app.psgi.
So the questions are:
- missed I something in the MetaCPAN (or in the docs), and here exists any (lighweight) web-framework which allows develop applications mountable many times in the
app.psgi? - or i'm forced to develop
Just Another My Own PSGI Framework? (To be honest, I not checked the catalyst - as it is too heavy-weight) - or just badly understand the "mounting"?
Building the dispatcher in Plack
There is an alternative to Plack::App::URLMap called Plack::App::HostMap that does the lookups way faster because it uses a hash internally, not an array. So there is no iterating going on. It just does a hash lookup, and those are really fast in Perl.
The trade-off is that now you can only use constant host names. So if your list is something like this:
Or with sub-domains like:
Then this is perfect. On the other hand I am not sure if it supports URLs that also have a constant path part, like
http://foo.example.org/bar, where there are lots offoos, but all of them share the same/barpath where the app is mounted. The module does not have any tests at all, and I couldn't try it. If you look at the changes, there has at least been one person suggesting additional features, so someone other than the author is using it.To use it, you would switch from Plack::Builder to using the the Plack::App::HostMap as an app that you call methods on.
You're not telling us what the
/route should do, but essentially it also needs a host. If your server has a lot of hostnames then all of them will respond to this request. That's the whole idea of what you want to do. But what hostname is for/? So the best thing to do would be to include an additional line for thesub { ... }slash-app with the real hostname. Maybe that's a control panel or something. So hook it up to the actual URL.A web framework to do this with
The singleton is not really the problem here. It seems not possible to get Dancer2 to load different configs or environments with the same one. I have not tried Mojo, Web::Simple or Catalyst for this use case.
I did try a lot with D2, and the closest I got was having a
/route in MyApp, and this PSGI app. Note this does not work.It uses the default skeleton generated with
dancer2 -a MyAppand unchanged environment files. The dispatching from Plack works, but Dancer2 gets confused.The idea was to use the same package file and subclass it to get the different config in via
with.However, it is possible to just define the same app in the loop, over and over again. You could probably move the route handler out use a code ref like
get '/' => \&main::get_slash, wheresub get_slashis not in theeval.The string
evalis not as evil as it looks here as that code only gets run at startup. D2 will internally keep track of all the apps that you created programmatically here. But I have no idea how performant that is.