2022-07-22 22:37:31 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# PostText v0.1
|
|
|
|
# Jul 22
|
|
|
|
|
|
|
|
use Mojolicious::Lite -signatures;
|
2022-07-29 01:13:26 +00:00
|
|
|
use Mojo::Pg;
|
|
|
|
|
|
|
|
# Load the local modules too
|
|
|
|
use lib 'lib';
|
|
|
|
use PostText::Model::Thread;
|
2022-08-05 02:46:35 +00:00
|
|
|
#use Data::Dumper; # For your debugging pleasure
|
2022-07-29 01:13:26 +00:00
|
|
|
|
|
|
|
# Load Mojo plugins
|
|
|
|
plugin 'Config';
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
helper pg => sub {
|
|
|
|
state $pg = Mojo::Pg->new(app->config->{app->mode}{'pg_string'})
|
|
|
|
};
|
|
|
|
|
2022-08-04 03:44:34 +00:00
|
|
|
helper thread => sub {
|
|
|
|
state $thread = PostText::Model::Thread->new(pg => shift->pg)
|
|
|
|
};
|
|
|
|
|
2022-07-29 01:13:26 +00:00
|
|
|
# Begin routing
|
|
|
|
under sub ($c) {
|
2022-08-01 03:17:15 +00:00
|
|
|
$c->session(expires => time() + 31536000);
|
2022-07-29 01:13:26 +00:00
|
|
|
|
|
|
|
1;
|
|
|
|
};
|
2022-07-22 22:37:31 +00:00
|
|
|
|
2022-07-30 03:30:19 +00:00
|
|
|
# Root
|
2022-07-29 00:44:47 +00:00
|
|
|
get '/', sub ($c) { $c->redirect_to('view') };
|
|
|
|
|
2022-07-30 03:30:19 +00:00
|
|
|
# View
|
2022-07-29 00:44:47 +00:00
|
|
|
get '/view', sub ($c) {
|
2022-08-05 02:46:35 +00:00
|
|
|
my $threads = $c->thread->get_threads();
|
|
|
|
|
|
|
|
$c->stash(threads => $threads);
|
|
|
|
|
|
|
|
$c->render();
|
2022-07-22 22:37:31 +00:00
|
|
|
};
|
|
|
|
|
2022-07-30 03:30:19 +00:00
|
|
|
# Post
|
|
|
|
any [qw{GET POST}], '/post', sub ($c) {
|
2022-08-06 06:26:53 +00:00
|
|
|
my $v;
|
2022-08-04 03:44:34 +00:00
|
|
|
|
2022-08-06 06:26:53 +00:00
|
|
|
$v = $c->validation() if $c->req->method eq 'POST';
|
|
|
|
|
|
|
|
if ($v && $v->has_data) {
|
|
|
|
my $thread_author = $c->param('name' ) || 'Anonymous';
|
|
|
|
my $thread_title = $c->param('title');
|
|
|
|
my $thread_body = $c->param('post' );
|
|
|
|
|
|
|
|
$v->required('name' )->size(1, 63 );
|
|
|
|
$v->required('title')->size(1, 127 );
|
|
|
|
$v->required('post' )->size(2, 4000);
|
|
|
|
|
|
|
|
$c->thread->create_thread($thread_author, $thread_title, $thread_body)
|
|
|
|
unless $v->has_error();
|
2022-08-04 03:44:34 +00:00
|
|
|
|
|
|
|
return $c->redirect_to('view');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c->render();
|
2022-07-30 03:30:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Configure things
|
2022-07-29 01:13:26 +00:00
|
|
|
app->secrets(app->config->{'secrets'}) || die $@;
|
|
|
|
|
2022-07-31 04:39:50 +00:00
|
|
|
app->pg->migrations->from_dir('migrations')->migrate(3);
|
2022-07-30 02:15:14 +00:00
|
|
|
|
2022-07-30 03:30:19 +00:00
|
|
|
# Send it
|
2022-07-22 22:37:31 +00:00
|
|
|
app->start();
|