Async-Redis

 view release on metacpan or  search on metacpan

examples/pagi-chat/lib/ChatApp/HTTP.pm  view on Meta::CPAN


use Cwd qw(abs_path);

my $JSON = JSON::MaybeXS->new->utf8->canonical;
my $PUBLIC_DIR = abs_path(File::Spec->catdir(dirname(__FILE__), '..', '..', 'public'));

# Debug: print the public dir on load
print STDERR "[HTTP] PUBLIC_DIR = $PUBLIC_DIR\n";

my %MIME_TYPES = (
    html => 'text/html; charset=utf-8',
    css  => 'text/css; charset=utf-8',
    js   => 'application/javascript; charset=utf-8',
    json => 'application/json; charset=utf-8',
    png  => 'image/png',
    ico  => 'image/x-icon',
);

sub handler {
    return async sub {
        my ($scope, $receive, $send) = @_;
        my $path = $scope->{path} // '/';
        my $method = $scope->{method} // 'GET';

examples/pagi-chat/lib/ChatApp/HTTP.pm  view on Meta::CPAN

        warn "API error: $err";
        return Future->done({ status => 500, data => { error => 'Internal error' } });
    });

    my $body = $JSON->encode($result->{data});

    await $send->({
        type    => 'http.response.start',
        status  => $result->{status},
        headers => [
            ['content-type', 'application/json; charset=utf-8'],
            ['content-length', length($body)],
        ],
    });

    await $send->({
        type => 'http.response.body',
        body => $body,
    });
}

examples/pagi-chat/public/index.html  view on Meta::CPAN

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PAGI Chat - Multi-User Chat Demo</title>
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <!-- Login Screen -->
    <div id="login-screen" class="screen">
        <div class="login-container">
            <div class="login-header">
                <h1>PAGI Chat</h1>

examples/slow-redis/app.pl  view on Meta::CPAN

Run multiple concurrent requests - they should all complete in ~1 second total!

Test with:
  for i in 1 2 3 4 5; do curl -s http://localhost:5001/ & done; wait
EOF

    await $send->({
        type    => 'http.response.start',
        status  => 200,
        headers => [
            ['content-type', 'text/plain; charset=utf-8'],
            ['x-worker-pid', "$worker"],
            ['x-elapsed', $elapsed],
        ],
    });
    await $send->({
        type => 'http.response.body',
        body => $body,
    });
}



( run in 1.071 second using v1.01-cache-2.11-cpan-697924b945d )