Async-Redis
view release on metacpan or search on metacpan
examples/pagi-chat/app.pl view on Meta::CPAN
# Route WebSocket
if ($type eq 'websocket' && $path eq '/ws/chat') {
return await $ws_handler->($scope, $receive, $send);
}
# Route HTTP
if ($type eq 'http') {
return await $http_handler->($scope, $receive, $send);
}
# SSE not implemented - return 404
if ($type eq 'sse') {
await $send->({ type => 'http.response.start', status => 404, headers => [] });
await $send->({ type => 'http.response.body', body => 'SSE not implemented' });
return;
}
die "Unsupported scope type: $type";
});
async sub _handle_lifespan {
my ($scope, $receive, $send) = @_;
examples/pagi-chat/lib/ChatApp/HTTP.pm view on Meta::CPAN
data => [
map { { name => $_, users => scalar(keys %{$rooms->{$_}{users}}) } }
sort keys %$rooms
],
};
}
if ($path =~ m{^/api/room/([^/]+)/history$} && $method eq 'GET') {
my $room_name = $1;
my $room = await get_room($room_name);
return { status => 404, data => { error => 'Room not found' } } unless $room;
return { status => 200, data => await get_room_messages($room_name, 100) };
}
if ($path =~ m{^/api/room/([^/]+)/users$} && $method eq 'GET') {
my $room_name = $1;
my $room = await get_room($room_name);
return { status => 404, data => { error => 'Room not found' } } unless $room;
return { status => 200, data => await get_room_users($room_name) };
}
if ($path eq '/api/stats' && $method eq 'GET') {
return { status => 200, data => await get_stats() };
}
return { status => 404, data => { error => 'Not found' } };
}
async sub _serve_static {
my ($scope, $receive, $send, $path) = @_;
$path = '/index.html' if $path eq '/';
$path =~ s/\.\.//g;
$path =~ s|//+|/|g;
my $file_path = File::Spec->catfile($PUBLIC_DIR, $path);
print STDERR "[HTTP] Serving: $file_path (exists: " . (-f $file_path ? 'yes' : 'no') . ")\n";
unless (-f $file_path && -r $file_path) {
return await _send_404($send);
}
my ($ext) = $file_path =~ /\.(\w+)$/;
my $content_type = $MIME_TYPES{lc($ext // '')} // 'application/octet-stream';
my $content;
{
open my $fh, '<:raw', $file_path or return await _send_500($send);
local $/;
$content = <$fh>;
examples/pagi-chat/lib/ChatApp/HTTP.pm view on Meta::CPAN
['content-length', length($content)],
],
});
await $send->({
type => 'http.response.body',
body => $content,
});
}
async sub _send_404 {
my ($send) = @_;
my $body = '{"error":"Not found"}';
await $send->({
type => 'http.response.start',
status => 404,
headers => [['content-type', 'application/json']],
});
await $send->({ type => 'http.response.body', body => $body });
}
async sub _send_500 {
my ($send) = @_;
my $body = '{"error":"Internal server error"}';
await $send->({
type => 'http.response.start',
examples/slow-redis/app.pl view on Meta::CPAN
my $path = $scope->{path} // '/';
my $method = $scope->{method} // 'GET';
# Handle lifespan events
if ($type eq 'lifespan') {
return await _handle_lifespan($scope, $receive, $send);
}
# Only handle HTTP
unless ($type eq 'http') {
await $send->({ type => 'http.response.start', status => 404, headers => [] });
await $send->({ type => 'http.response.body', body => 'Not Found' });
return;
}
# Route: GET /
if ($path eq '/' && $method eq 'GET') {
return await _handle_slow_request($scope, $receive, $send);
}
# Route: GET /fast (no delay, for comparison)
if ($path eq '/fast' && $method eq 'GET') {
return await _handle_fast_request($scope, $receive, $send);
}
# 404 for other paths
await $send->({ type => 'http.response.start', status => 404, headers => [] });
await $send->({ type => 'http.response.body', body => 'Not Found' });
};
# Slow request handler - delays 1 second, then returns Redis TIME
async sub _handle_slow_request {
my ($scope, $receive, $send) = @_;
my $start = time();
my $worker = $$;
( run in 0.809 second using v1.01-cache-2.11-cpan-39bf76dae61 )