NAME
PAGI::Middleware::Builder - DSL for composing PAGI middleware
SYNOPSIS
use PAGI::Middleware::Builder;
# Functional DSL
my $app = builder {
enable 'ContentLength';
enable 'CORS', origins => ['*'];
enable_if { $_[0]->{path} =~ m{^/api/} } 'RateLimit', limit => 100;
mount '/static' => $static_app;
$my_app;
};
# Object-oriented interface
my $builder = PAGI::Middleware::Builder->new;
$builder->enable('ContentLength');
$builder->enable('CORS', origins => ['*']);
$builder->mount('/admin', $admin_app);
my $app = $builder->to_app($my_app);
DESCRIPTION
PAGI::Middleware::Builder provides a DSL for composing middleware into a PAGI application. It supports:
Enabling middleware with configuration
Conditional middleware application
Path-based routing (mount)
Middleware ordering
EXPORTS
builder
my $app = builder { ... };
Create a composed application using the DSL. The block should call enable(), enable_if(), mount(), and return the final app.
enable
enable 'MiddlewareName', %config;
enable 'PAGI::Middleware::Custom', %config;
Enable a middleware. If the name doesn't contain '::', it's prefixed with 'PAGI::Middleware::'.
enable_if
enable_if { $condition } 'MiddlewareName', %config;
Conditionally enable middleware. The condition block receives the scope and returns true/false.
mount
mount '/path' => $app;
Mount an application at a path prefix. Requests matching the prefix are routed to the mounted app with adjusted paths.
METHODS
new
my $builder = PAGI::Middleware::Builder->new;
Create a new builder instance.
enable
$builder->enable('MiddlewareName', %config);
Add middleware to the stack (OO interface).
enable_if
$builder->enable_if(\&condition, 'MiddlewareName', %config);
Add conditional middleware to the stack (OO interface).
mount
$builder->mount('/path', $app);
Add a path-based mount point (OO interface).
to_app
my $app = $builder->to_app($inner_app);
Build the composed application.
MIDDLEWARE ORDERING
Middleware is applied in the order specified, with the first middleware being the outermost wrapper. This means:
builder {
enable 'A';
enable 'B';
enable 'C';
$app;
};
Results in: A wraps B wraps C wraps $app
Request flow: A -> B -> C -> app -> C -> B -> A
SEE ALSO
PAGI::Middleware - Base class for middleware