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

HTTP::Engineでsessionを扱う方法

#!/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(
        method_class => 'HTTP::Engine::Request' );
    $mw->install(
        'HTTP::Engine::Middleware::HTTPSession' => {
            store => {
                class => 'File',
                args  => { dir => '/Users/dann/tmp', }
            },
            state => {
                class => 'Cookie',
                args  => { session_id_name => 'dann', },
            }
        }
    );  
    $mw->handler( \&request_handler );
}


sub request_handler {
    my $req      = shift;
    my $response = HTTP::Engine::Response->new;
    my $session  = $req->session;
    my $body     = 'All you can do is try very hard to be at the right place and be ready';
    $response->body($body);
    $response->status(200);
    $response;
}