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';
|
2021-12-05 02:52:31 -05:00
|
|
|
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';
|
2021-12-11 21:02:54 -05:00
|
|
|
plugin 'TagHelpers::Pagination';
|
2021-12-04 18:34:35 -05:00
|
|
|
|
|
|
|
# Helpers
|
|
|
|
helper pg => sub {
|
2021-12-19 00:43:18 -05:00
|
|
|
my $env = app->mode() eq 'development' ? 'dev_env' : 'prod_env';
|
2021-12-11 18:48:00 -05:00
|
|
|
|
2021-12-04 18:34:35 -05:00
|
|
|
state $pg = Mojo::Pg->new(
|
2021-12-19 17:43:58 -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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-12-04 23:06:05 -05:00
|
|
|
helper message => sub {
|
2021-12-05 02:52:31 -05:00
|
|
|
state $message = GuestbookNg::Model::Message->new(pg => shift->pg)
|
2021-12-04 23:06:05 -05:00
|
|
|
};
|
|
|
|
|
2021-12-05 04:22:55 -05:00
|
|
|
# Routes
|
2021-12-18 23:03:53 -05:00
|
|
|
under sub ($c) {
|
|
|
|
# Opt out of Google FLoC
|
|
|
|
# https://paramdeo.com/blog/opting-your-website-out-of-googles-floc-network
|
|
|
|
$c->res->headers->header('Permissions-Policy', 'interest-cohort=()');
|
|
|
|
|
|
|
|
1;
|
|
|
|
};
|
|
|
|
|
2021-12-04 00:11:37 -05:00
|
|
|
get '/' => sub ($c) {
|
2021-12-11 21:02:54 -05:00
|
|
|
my $posts = $c->message->get_posts();
|
2021-12-12 01:50:07 -05:00
|
|
|
my $last_page = $c->message->get_last_page(@$posts);
|
2021-12-19 17:20:58 -05:00
|
|
|
my $this_page = $c->param('page') || 1;
|
2021-12-12 01:50:07 -05:00
|
|
|
my @view_posts = $c->message->view_posts($this_page, $last_page, @$posts);
|
2021-12-05 02:52:31 -05:00
|
|
|
|
2021-12-11 21:02:54 -05:00
|
|
|
$c->stash(
|
|
|
|
view_posts => \@view_posts,
|
|
|
|
this_page => $this_page,
|
|
|
|
last_page => $last_page
|
|
|
|
);
|
|
|
|
|
|
|
|
$c->render();
|
2021-12-04 02:26:25 -05:00
|
|
|
} => 'index';
|
|
|
|
|
2021-12-19 00:43:18 -05:00
|
|
|
any [qw{GET POST}], '/sign' => sub ($c) {
|
2021-12-05 02:52:31 -05:00
|
|
|
if ($c->req->method() eq 'POST') {
|
|
|
|
my $name = $c->param('name');
|
|
|
|
my $message = $c->param('message');
|
2021-12-18 23:03:53 -05:00
|
|
|
my $answer = $c->param('answer');
|
2021-12-05 02:52:31 -05:00
|
|
|
|
2021-12-18 23:03:53 -05:00
|
|
|
$c->message->send_post($name, $message) if $answer;
|
2021-12-05 02:52:31 -05:00
|
|
|
$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-12 01:50:07 -05:00
|
|
|
|
|
|
|
app->message->max_posts(app->config->{'max_posts'})
|
|
|
|
if app->config->{'max_posts'};
|
|
|
|
|
|
|
|
app->pg->migrations->from_dir('migrations')->migrate(1);
|
|
|
|
|
2021-12-04 00:11:37 -05:00
|
|
|
app->start();
|