Files
compiled.world/lib/CW.pm
2026-08-11 23:10:22 -06:00

61 lines
1.2 KiB
Perl

# This is the main class for compiled.world, it is instantiated every request
# Routes, database connections, and other data around the request lifecycle are passed
# as attributes, and should not be instantiated per request.
package CW;
use Moo;
use Plack::Request;
use CW::Plack::Request;
use CW::Plack::Response;
has h => (
is => 'ro',
required => 1
);
has dbh => (
is => 'ro',
required => 1
);
has environment => (
is => 'ro',
required => 1
);
has router => (
is => 'ro',
required => 1
);
sub app {
my ($self) = @_;
return sub {
my ($env) = @_;
my $plack_request = Plack::Request->new($env);
my $request =
CW::Plack::Request->new( request => $plack_request, h => $self->h );
my $response = $request->new_response(200);
my $match = $self->router->match($env);
if ($match) {
my $route = $match->{route};
my $pkg = $route->pkg;
my $action = $route->action;
$pkg->new->$action( $request, $response );
}
else {
$response->status(404);
$response->body("404 not found");
}
return $response->finalize;
}
}
1;