guestbook-ng/guestbook-ng.pl

77 lines
1.6 KiB
Perl
Raw Normal View History

2021-12-04 00:11:37 -05:00
#!/usr/bin/env perl
# Dec 2021
# Daniel Bowling <swaggboi@slackware.uk>
use Mojolicious::Lite -signatures;
2021-12-04 18:34:35 -05:00
use Mojo::Pg;
2021-12-04 02:26:25 -05:00
use lib 'lib';
use GuestbookNg::Model::Test;
use GuestbookNg::Model::Message;
use Data::Dumper; # Uncomment for debugging
2021-12-04 02:26:25 -05:00
2021-12-04 18:34:35 -05:00
# Plugins
plugin 'Config';
# Helpers
helper pg => sub {
state $pg = Mojo::Pg->new(
2021-12-04 18:50:19 -05:00
'postgres://' .
2021-12-04 18:34:35 -05:00
app->config->{'pg_user'} .
2021-12-04 18:50:19 -05:00
':' .
2021-12-04 18:34:35 -05:00
app->config->{'pg_pw'} .
2021-12-04 22:33:53 -05:00
'@' .
app->config->{'pg_host'} .
'/' .
2021-12-04 18:34:35 -05:00
app->config->{'pg_db'}
);
};
helper test => sub {
state $test = GuestbookNg::Model::Test->new(pg => shift->pg)
};
2021-12-04 23:06:05 -05:00
helper message => sub {
state $message = GuestbookNg::Model::Message->new(pg => shift->pg)
2021-12-04 23:06:05 -05:00
};
2021-12-04 18:34:35 -05:00
# Routes
under sub ($c) {
2021-12-04 23:06:05 -05:00
$c->pg->migrations->from_dir('migrations')->migrate(1);
2021-12-04 18:34:35 -05:00
};
2021-12-04 00:11:37 -05:00
get '/' => sub ($c) {
my $posts = $c->message->get_posts();
$c->render(posts => $posts);
2021-12-04 02:26:25 -05:00
} => 'index';
any '/test' => sub ($c) {
2021-12-04 13:28:07 -05:00
my $method = $c->req->method();
2021-12-04 18:34:35 -05:00
my $time = $c->test->now();
my $string =
$method eq 'POST' ? $c->test->test_model($c->param('string')) : undef;
2021-12-04 02:26:25 -05:00
2021-12-04 18:34:35 -05:00
$c->render(
method => $method,
string => $string,
time => $time
);
2021-12-04 00:11:37 -05:00
};
any '/post' => sub ($c) {
if ($c->req->method() eq 'POST') {
my $name = $c->param('name');
my $message = $c->param('message');
$c->message->send_post($name, $message);
$c->redirect_to('index');
}
else {
$c->render()
}
};
2021-12-04 18:34:35 -05:00
# Send it
2021-12-04 00:11:37 -05:00
app->start();