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:
example.org
example.com
example.de
example.am
example.cx
Or with sub-domains like:
one.example.org
two.example.org
three.example.org
four.example.org
five.example.org
six.example.org
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 of foo
s, but all of them share the same /bar
path 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.
use Plack::App::HostMap;
# set up %apps (e.g. foo.example.org, bar.example.org)
my $host_map = Plack::App::HostMap->new;
for my $site (@sites) {
$host_map->map( $site => $apps->{$site} );
}
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 the sub { ... }
slash-app with the real hostname. Maybe that's a control panel or something. So hook it up to the actual URL.
$host_map->map( "example.org" => sub { ... } );
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.
use Plack::Builder;
my $builder = Plack::Builder->new;
foreach my $name (qw/development production/) {
$builder->mount(
"/$name" => builder {
eval <<"APP";
package MyApp::$name {
use Dancer2;
use MyApp with => { environment => "$name" };
}
APP
"MyApp::$name"->to_app;
}
);
}
$builder->to_app;
It uses the default skeleton generated with dancer2 -a MyApp
and unchanged environment files. The dispatching from Plack works, but Dancer2 gets confused.
HTTP::Server::PSGI: Accepting connections at http://0:5000/
[MyApp::production:4896] core @2017-02-10 02:14:42> looking for get / in /home/julien/perl5/perlbrew/perls/perl-5.20.1/lib/site_perl/5.20.1/Dancer2/Core/App.pm l. 35
[MyApp::production:4896] core @2017-02-10 02:14:42> Entering hook core.error.init in (eval 49) l. 1
[MyApp::production:4896] core @2017-02-10 02:14:42> Entering hook core.error.before in (eval 49) l. 1
[MyApp::production:4896] core @2017-02-10 02:14:42> Entering hook core.error.after in (eval 49) l. 1
127.0.0.1 - - [10/Feb/2017:02:14:42 +0100] "GET /production/ HTTP/1.1" 404 456 "-" "curl/7.47.0"
[MyApp::development:4896] core @2017-02-10 02:18:06> looking for get in /home/julien/perl5/perlbrew/perls/perl-5.20.1/lib/site_perl/5.20.1/Dancer2/Core/App.pm l. 35
[MyApp::development:4896] core @2017-02-10 02:18:06> Entering hook core.error.init in (eval 49) l. 1
[MyApp::development:4896] core @2017-02-10 02:18:06> Entering hook core.error.before in (eval 49) l. 1
[MyApp::development:4896] core @2017-02-10 02:18:06> Entering hook core.error.after in (eval 49) l. 1
127.0.0.1 - - [10/Feb/2017:02:18:06 +0100] "GET /development HTTP/1.1" 404 457 "-" "curl/7.47.0"
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
, where sub get_slash
is not in the eval
.
use Plack::Builder;
my $builder = Plack::Builder->new;
foreach my $name (qw/development production/) {
$builder->mount(
"/$name" => builder {
eval <<"APP";
package MyApp::$name {
use Dancer2;
use Data::Printer;
set environment => "$name";
get "/" => sub { np(config) }
}
APP
"MyApp::$name"->to_app;
}
);
}
$builder->to_app;
The string eval
is 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.
Some::PsgiFramework::MyApp
and using$env->{HTTP_HOST}
(or the framework's equivalent) to customize each site? That's what I do for some multilingual sites. – SubversionSome::PsgiFramework:
? Which one suports the multi-site mounting on the framework level (build in support - not my own "hacking")? – Justeninstances
of the same app. (Please read the code above). When the app is instantiated it knows nothing about the$env->{HTTP_HOST}
. (because the request handling doesn't even started). – Justen