concrete
This commit is contained in:
60
lib/CW.pm
Normal file
60
lib/CW.pm
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
# 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;
|
||||
Reference in New Issue
Block a user