From c4bac0c3a7632a18647bf8e0d63112ca17de7a68 Mon Sep 17 00:00:00 2001 From: swag Date: Sat, 4 Dec 2021 18:34:35 -0500 Subject: [PATCH] Initial DB implementation --- guestbook-ng.pl | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/guestbook-ng.pl b/guestbook-ng.pl index f774e70..0a3c811 100755 --- a/guestbook-ng.pl +++ b/guestbook-ng.pl @@ -4,10 +4,33 @@ # Daniel Bowling use Mojolicious::Lite -signatures; +use Mojo::Pg; use lib 'lib'; use GuestbookNg::Model::Test; -my $tm = GuestbookNg::Model::Test->new(); +# Plugins +plugin 'Config'; + +# Helpers +helper pg => sub { + state $pg = Mojo::Pg->new( + 'postgres://' . + app->config->{'pg_user'} . + ':' . + app->config->{'pg_pw'} . + '@localhost/' . + app->config->{'pg_db'} + ); +}; + +helper test => sub { + state $test = GuestbookNg::Model::Test->new(pg => shift->pg) +}; + +# Routes +under sub ($c) { + $c->test->create_table() +}; get '/' => sub ($c) { $c->render() @@ -15,9 +38,16 @@ get '/' => sub ($c) { any '/test' => sub ($c) { my $method = $c->req->method(); - my $string = $tm->test_model($c->param('string')); + my $time = $c->test->now(); + my $string = + $method eq 'POST' ? $c->test->test_model($c->param('string')) : undef; - $c->render(method => $method, string => $string); + $c->render( + method => $method, + string => $string, + time => $time + ); }; +# Send it app->start();