perl使用nginx fastcgi環(huán)境做web開發(fā)實例

字號:


    hello world
    一個簡單的hello world例子:
    代碼如下:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    use cgi::fast;
    while(my $q = new cgi::fast)
    {
    print $q->header(text/plain);
    print hello world;
    }
    和cgi的區(qū)別僅在于多了一個循環(huán)來接受請求,cgi::fast對象和cgi接口是一樣的,而且該腳本也可以當(dāng)做cgi腳本使用。
    搭建nginx + fastcgi 環(huán)境
    perl使用cgi::fast包來提供fastcgi服務(wù),該包提供兩種方式來啟動fastcgi進程,一個是直接使用該包提供的服務(wù)將當(dāng)前進程變?yōu)閒astcgi進程,另外一個是使用第三方工具spawn-fcgi來啟動。
    nginx配置方式例子:
    代碼如下:
    location / {
    fastcgi_pass 127.0.0.1:8184;
    fastcgi_param script_filename /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
    配置好nginx后,使用spawn-fcgi來啟動前面的hello world:
    代碼如下:
    $ spawn-fcgi -n -a 127.0.0.1 -p 8184 -f ./main.pl
    調(diào)試支持
    在前面的命令行里使用了參數(shù)-n,讓spawn-fcgi不要fork出多個進程,并阻塞,允許用戶ctrl+c來關(guān)閉,產(chǎn)品服務(wù)器可以去掉這個參數(shù)來充分利用服務(wù)器的多核來提供更高的并發(fā)數(shù)。我之前寫了一個bash腳本,允許在文件改動的情況下重啟服務(wù),方便調(diào)試perl程序,代碼如下:
    代碼如下:
    #!/bin/bash
    #pid文件和需要啟動的腳本
    pid_file=service.pid
    main=main.pl
    #關(guān)閉之前啟動的進程
    term() {
    test -e $pid_file || return
    pid=`cat $pid_file`
    kill -s -0 $pid || return
    echo terminating $main $pid
    rm -f $pid_file
    kill $pid
    wait $pid
    }
    #當(dāng)前腳本退出的時候也關(guān)閉啟動了的fastcgi進程
    trap term;exit sigint sigterm
    while true
    do
    #首次啟動或者文件改動后都需要關(guān)閉之前的進程
    term
    #以no fork方式啟動腳本來調(diào)試,并將pid寫入到文件
    spawn-fcgi -n -a 127.0.0.1 -p 8184 -f ./$main &
    pid=$!
    echo $pid > $pid_file
    echo my perl service started, pid = $pid
    #監(jiān)控文件變化
    files=`find . -name '*.pl' -o -name '*.pm' -o -name '*.html'`
    md5=`md5sum $files|md5sum`
    #wait for file change
    while [[ `md5sum $files|md5sum` = $md5 ]]
    do
    sleep 1
    done
    echo file changes detected, restarting service
    done
    該腳本已在mac osx和linux下測試通過
    路由系統(tǒng)
    做web開發(fā)離不開路由實現(xiàn),來對不同請求來做出特定的響應(yīng)。
    路由請求依賴http method和uri兩部分,因此主要就是需要這兩者來做分派。
    在cgi中可以通過環(huán)境變量request_method和request_uri來獲取請求方法和uri。
    因此一個簡單的路由系統(tǒng)實際上可以分解為一個二級的map,注冊路由實際上就是往這個map里放入規(guī)則對應(yīng)的處理函數(shù),而分派請求則是從這個map里根據(jù)規(guī)則獲取對應(yīng)的處理函數(shù),一個簡單的例子:
    代碼如下:
    my %routers = ();
    sub not_found
    {
    print status: 404\n;
    print content-type: text/html\n\n;
    print<<eof
    <html>
    <body>
    <h1>404 not found</h1>
    cannot find $env{request_path}.
    </body>
    </html>
    eof
    }
    sub add_rule
    {
    my ($method, $path, $callback) = @_;
    my $handlers = $routers{$method};
    $handlers = $routers{$method} = {} if not $handlers;
    $handlers->{$path} = $callback;
    }
    sub dispatch
    {
    my $q = shift;
    my $method = $env{request_method};
    my $uri = $env{request_uri};
    $uri =~ s/\?.*$//;
    my $handler = ($routers{$method} || {})->{$uri} || not_found;
    eval
    {
    &$handler($q);
    };
    print stderr failed to handle $method $uri: if $@;
    }
    使用這個路由系統(tǒng)的例子:
    代碼如下:
    sub index
    {
    my ($q) = @_;
    print $q->header('text/plain');
    print hello world!;
    }
    router::add_rule('get', '/', \&index);
    模板系統(tǒng)
    perl提供了大量的模板系統(tǒng)的實現(xiàn),我個人最喜歡的是template toolkit,文檔也非常豐富,網(wǎng)站是 。
    將前面的index修改為使用模板的例子:
    代碼如下:
    use template;
    my $tt = new template({include_path => 'templates', interpolate => 1});
    sub index
    {
    my ($q) = @_;
    my $output = '';
    print $q->header('text/html');
    $tt->process('index.html', {world => 'world'}, $output) || die $tt->error();
    print $output;
    }
    其中templates/index.html文件內(nèi)容如下:
    代碼如下:
    <html>
    <head><title>demo</title></head>
    <body>
    hello ${world}
    </body>
    </html>
    完!