This repository has been archived by the owner on Feb 20, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
very stupid template rendering. needs massive refactoring
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package Prong::Server::Gadget; | ||
use Moose; | ||
use Prong::Schema; | ||
use Plack::Request; | ||
use Router::Simple; | ||
use Text::MicroTemplate::File; | ||
use namespace::autoclean; | ||
|
||
has router => ( | ||
is => 'ro', | ||
isa => 'Router::Simple', | ||
lazy_build => 1, | ||
); | ||
|
||
has template => ( | ||
is => 'ro', | ||
isa => 'Text::MicroTemplate::File', | ||
required => 1, | ||
); | ||
|
||
has schema => ( | ||
is => 'ro', | ||
isa => 'Prong::Schema', | ||
required => 1, | ||
); | ||
|
||
sub _build_router { | ||
my $self = shift; | ||
|
||
my $router = Router::Simple->new(); | ||
$router->connect('/' => { | ||
code => sub { | ||
my $req = shift; | ||
my @modules = $self->schema->resultset('Module')->search(); | ||
return [ | ||
200, | ||
[ "Content-Type" => "text/html" ], | ||
[ $self->template->render_file( 'index.mt', $req, { modules => \@modules } ) ] | ||
]; | ||
} | ||
} ); | ||
|
||
$router->connect('/app/{module_id}' => { | ||
code => sub { | ||
my ($req, $p) = @_; | ||
my $content = $self->schema->resultset('ModuleContent')->search({ | ||
module_id => $p->{module_id}, | ||
})->single; | ||
return [ | ||
200, | ||
[ "Content-Type" => "text/html" ], | ||
[ $self->template->render_file( 'app/view.mt', $req, { content => $content } ) ] | ||
]; | ||
} | ||
}); | ||
|
||
return $router; | ||
} | ||
|
||
sub process { | ||
my ($self, $env) = @_; | ||
|
||
if (my $p = $self->router->match( $env )) { | ||
return $p->{code}->( Plack::Request->new( $env ), $p ); | ||
} | ||
|
||
return [ 404, [], [ 'Not Found' ] ]; | ||
} | ||
|
||
sub psgi_app { | ||
my $self = shift; | ||
return sub { | ||
my $env = shift; | ||
$self->process( $env ); | ||
}; | ||
} | ||
|
||
__PACKAGE__->meta->make_immutable(); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use lib "lib"; | ||
use Prong::Server::Gadget; | ||
|
||
Prong::Server::Gadget->new( | ||
schema => Prong::Schema->connect( | ||
'dbi:mysql:dbname=prong', | ||
'root', | ||
undef | ||
), | ||
template => Text::MicroTemplate::File->new( | ||
use_cache => 1, | ||
include_path => [ | ||
'templates' | ||
] | ||
) | ||
)->psgi_app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
? my ($req, $stash) = @_; | ||
? $_mt->wrapper_file('wrapper.mt')->(sub { | ||
|
||
<?= Text::MicroTemplate::encoded_string($stash->{content}->content) ?> | ||
|
||
? }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
? my ($req, $stash) = @_; | ||
? $_mt->wrapper_file('wrapper.mt')->(sub { | ||
|
||
? foreach my $module (@{ $stash->{modules} }) { | ||
<a href="/app/<?= $module->id ?>"><?= $module->title ?></a> | ||
? } | ||
|
||
? }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<?= $_[0] ?> | ||
</body> | ||
</html> |