抄的一段笔记:用Plack实现git web访问
Plack::App::GitSmartHttp plackup -s Starman -p 500 git.psgi -D #!/usr/bin/perl use strict; use Plack::App::GitSmartHttp; Plack::App::GitSmartHttp->new( root => '/data/git', upload_pack => 1, # clone received_pack => 1, # push )->to_app; #!/usr/bin/perl use strict; use Plack::Builder; use Plack::App::GitSmartHttp; my $git = Plack::App::GitSmartHttp->new( root => '/data/git', upload_pack => 1, # clone received_pack => 1, # push ); my %user = ( 'test1' => 'test', ); builder { enable "Auth::Basic", authenticator => sub { my($username, $password) = @_; return 1 if exists $user{$username} and $user{$username} eq "$password"; }; $git; }; 在写一个配合可以根据每个用户设置权限的小例子 #!/usr/bin/perl use strict; use Plack::Builder; use Plack::App::URLMap; use Plack::App::GitSmartHttp; use Plack::Middleware::Auth::Basic; my %user_list = ( user1 => 'pass', user2 => 'pass', ); my $app = Plack::App::URLMap->new; # 单独的用户认证和访问地址 for my $user (keys %user_list) { my $user_path = "$user.git"; my $git_app = Plack::App::GitSmartHttp->new( root => "/data/git/$user_path", upload_pack => 1, # clone received_pack => 1, # push ); $git_app = Plack::Middleware::Auth::Basic->wrap( $git_app, authenticator => sub { my($username, $password) = @_; return 1 if $username eq $user and $user_list{$user} eq $password; } ); $app->mount("/$user_path" => $git_app ); } $app; # 如果想git 多用户分组管理同一个项目,也是非常容易的。 my $authority_list = { 'squid.git' => { 'user1' => { pass => 'user2pass', }, }, 'nginx.git' => { 'user2' => { pass => 'user2pass', }, }, }; my $app = Plack::App::URLMap->new; # 用户认证和访问地址 foreach my $git_path (keys %$authority_list) { my $git_app = Plack::App::GitSmartHttp->new( root => "/data/git/" . $git_path, upload_pack => 1, # clone received_pack => 1, # push ); $git_app = Plack::Middleware::Auth::Basic->wrap( $git_app, authenticator => sub { my($username, $password) = @_; my $git_user = $authority_list->{$git_path}{$username} || ''; return 1 if $git_user and $git_user->{pass} eq $password; } ); $app->mount("/$git_path" => $git_app ); } $app;
自从Perl界出来PSGI的协议,大量的基于这种协议的应用都出来了,无论是性能,还是功能,还是开发速度。都有了极为大的提升。象前几天,我做了一个基于IP的视频文件的GSLB。用来做基于用户IP的视频访问调度的系统。原来是使用Nginx加Lua开发,性能是不错,但周期非常有点小长。后来重新使用这种Plack的一个高性能的后端feersum。花了一天时间就重新开发了一个,目前可以每秒处理10k的调度请求的应用。所以基于Plack来实现各种接口和应用相当的容易和高效。