72 lines
2.1 KiB
Perl
72 lines
2.1 KiB
Perl
package MyApp;
|
|
use Mojo::Base 'Mojolicious', -signatures;
|
|
|
|
# Define home-made helpers.
|
|
sub add_my_helpers ($self) {
|
|
$self->helper(
|
|
commify => sub ($self, $number) {
|
|
my $rev = reverse $number;
|
|
while ($rev =~ s/(\d{3})(\d)/$1,$2/) {};
|
|
return scalar reverse $rev;
|
|
}
|
|
);
|
|
$self->helper(
|
|
default_rpp => sub ($self) {
|
|
return $self->config->{'default-rows-per-page'};
|
|
}
|
|
);
|
|
$self->helper(
|
|
logname => sub ($self) { $self->session('logname'); }
|
|
);
|
|
$self->helper(
|
|
max_rpp => sub ($self) {
|
|
return $self->config->{'maximum-rows-per-page'};
|
|
}
|
|
);
|
|
$self->helper(
|
|
rpp => sub ($self) {
|
|
$self->session('rows_per_page') // $self->default_rpp;
|
|
}
|
|
);
|
|
return;
|
|
}
|
|
|
|
sub startup ($self) {
|
|
# Set a "secret" for signing cookies, preventing "Your secret
|
|
# passphrase needs to be changed" in the log, using my_app.yml.
|
|
my $config = $self->plugin('NotYAMLConfig');
|
|
$self->secrets([ $config->{secrets} ]);
|
|
|
|
# Add home-made helper(s).
|
|
$self->add_my_helpers;
|
|
|
|
# Set routes.
|
|
my $r = $self->routes;
|
|
# Home gets an explicit name, else its name is an empty string. A
|
|
# route's default name seems to come from '/path' with the leading
|
|
# '/' removed.
|
|
$r->get('/')->to('home#index')->name('home');
|
|
$r->get('/about')->to('home#about');
|
|
$r->get('/auth')->to('auth#index');
|
|
$r->post('/login')->to('auth#login');
|
|
$r->get('/logout')->to('auth#logout');
|
|
$r->get('/books/<page_number:num>')->to('book#books')->name('books');
|
|
$r->get('/authors/<page_number:num>')->to('book#authors')->name('authors');
|
|
$r->get('/author/<id:num>/<page_number:num>')->to('book#author')->name('author');
|
|
|
|
# Put a route under athentication.
|
|
my $auth = $r->under(
|
|
'/' => sub ($c) {
|
|
return 1 if ($c->logname);
|
|
$c->flash('naughty' => 1);
|
|
$c->session('post_login' => $c->current_route);
|
|
$c->redirect_to('auth');
|
|
return undef;
|
|
}
|
|
);
|
|
$auth->get('/account')->to('auth#account')->name('account');
|
|
$auth->post('/preferences')->to('auth#preferences')->name('preferences');
|
|
}
|
|
|
|
1;
|