読者です 読者をやめる 読者になる 読者になる

HTTP::Engine::Middleware::Staticで静的ファイルを扱う

開発用途にどうぞ。

#!/usr/bin/env perl
use HTTP::Engine;
use HTTP::Engine::Middleware;
 
main();
 
sub main {
    my $engine = setup_engine();
    $engine->run();
}
 
sub setup_engine {
    return HTTP::Engine->new(
        interface => {
            module => 'ServerSimple',
            args => { port => 3000, },
            request_handler => setup_request_handler(),
        },
    );
}
 
sub setup_request_handler {
    my $mw = HTTP::Engine::Middleware->new();
    $mw->install(
        'HTTP::Engine::Middleware::Static' => {
            regexp => qr{^/images/.+$},
            docroot => '/Users/dann/workdir/',
        }
    );
    $mw->handler( \&request_handler );
}
 
sub request_handler {
    my $req = shift;
    my $response = HTTP::Engine::Response->new;
    my $body = &#39;<html><body><img src="/images/logo.jpg"></img><body></html>&#39;;
 
    $response->body($body);
    $response->status(200);
    $response;
}