前回の質問で、マルチドメイン ソリューションについて尋ねましたが、質問が複雑すぎました。
要するに:
Apache のディレクティブのように、Starman (または他の純粋な perl PSGI サーバー) を使用して、名前ベースの仮想ホストを何らかの方法でセットアップすることは可能ですか? <VirtualHost ...>
それとも、この種の機能を得るために Apache を使用する必要がありますか?
何か案が?
ミドルウェアは、 Plack::App::URLMapを使用して、 Plack::Builderで既に実行されています。ポッドは次のように言っています。
URL をホスト名にマッピングすることも可能で、その場合、URL マッピングは仮想ホストのように機能します。
構文は 3 番目のマウントにあります。
builder {
mount "/foo" => builder {
enable "Plack::Middleware::Foo";
$app;
};
mount "/bar" => $app2;
mount "http://example.com/" => builder { $app3 };
};
ここに例を示します: 一部のサイト用の 1 つのモジュール (アプリ)。
lib/YourApp.pm は次のようになります。
package YourApp;
use strict;
use warnings;
use Dancer ':syntax';
setting apphandler => 'PSGI';
Dancer::App->set_running_app('YourApp');
# This and other routes ...
get '/' => sub {
# Static and template files will be from different directories are
# based by host http header
template 'index';
};
1;
bin/app.psgi は次のようになります。
#!/usr/bin/perl
use strict;
use warnings;
use Dancer;
# The next line can miss but need for quickly loading in L<Starman> server
use YourApp;
use Plack::Builder;
# Please notice that here no need ports in url
# So for http://app1.foo.com:3000/ will work
# http://app1.foo.com/
my $hosts = {
'http://app1.foo.com/' => '/appdir/1',
'http://app2.foo.com/' => '/appdir/2'
};
builder {
my $last;
foreach my $host (keys %$hosts) {
$last = mount $host => sub {
my $env = shift;
local $ENV{DANCER_APPDIR} = $hosts->{$host};
load_app "YourApp";
Dancer::App->set_running_app('YourApp');
setting appdir => $hosts->{$host};
Dancer::Config->load;
my $request = Dancer::Request->new( env => $env );
Dancer->dance($request);
};
}
$last;
};
この私のモジュールを試すことができます-ビルダーとマッピングよりも仮想ホスティングの方が簡単だと思います: