guestbook-ng/guestbook-ng.pl

80 lines
1.8 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 {
2021-12-11 18:48:00 -05:00
my $env =
app->mode eq 'development' ? 'dev_env' : 'prod_env';
2021-12-04 18:34:35 -05:00
state $pg = Mojo::Pg->new(
2021-12-11 18:48:00 -05:00
'postgres://' .
app->config->{$env}->{'pg_user'} .
':' .
app->config->{$env}->{'pg_pw'} .
'@' .
app->config->{$env}->{'pg_host'} .
'/' .
app->config->{$env}->{'pg_db'}
2021-12-04 18:34:35 -05:00
);
};
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
};
# Get the DB ready
app->pg->migrations->from_dir('migrations')->migrate(1);
2021-12-04 00:11:37 -05:00
# Routes
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-05 03:32:22 -05:00
app->secrets(app->config->{'secrets'}) || die $@;
2021-12-04 00:11:37 -05:00
app->start();