2011-05-18 107 views

回答

10

該中間件已在Plack::Builder中完成,Plack::App::URLMap。該吊艙俗話說:

URL映射與主機名也 可能的,在這種情況下,URL映射 就像一個虛擬主機。

語法是在第三安裝:

builder { 
     mount "/foo" => builder { 
      enable "Plack::Middleware::Foo"; 
      $app; 
     }; 

     mount "/bar" => $app2; 
     mount "http://example.com/" => builder { $app3 }; 
    }; 
+3

我的天啊。需要更多的RTFM。感謝名單。 – jm666 2011-05-18 15:02:23

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; 
    }; 

你可以嘗試這是我的模塊 - 我認爲它會更容易對虛擬主機不是建設者&映射:

https://github.com/Perlover/Dancer-Plugin-Hosts