62 lines
1.3 KiB
Perl
62 lines
1.3 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;
|
|
use CW::Migration;
|
|
|
|
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( dbh => $self->dbh )->$action( $request, $response );
|
|
}
|
|
else {
|
|
$response->status(404);
|
|
$response->body("404 not found");
|
|
}
|
|
|
|
return $response->finalize;
|
|
}
|
|
}
|
|
|
|
1;
|