Skip to content

Instantly share code, notes, and snippets.

@jberger
Created February 21, 2015 16:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/c69599f61c67b6149af8 to your computer and use it in GitHub Desktop.
Save jberger/c69599f61c67b6149af8 to your computer and use it in GitHub Desktop.
package LegendApp;
use Mojo::Base 'Mojolicious';
use experimental qw/signatures postderef/;
use DBM::Deep;
use LegendApp::Artwork;
has db => sub { DBM::Deep->new('legend.db') };
has artwork => sub { LegendApp::Artwork->new(shift->home->rel_dir('artwork')) };
sub startup ($app) {
my $r = $app->routes;
$r->add_shortcut(authorized => sub ($r, @) {
return $r->under(sub ($c) {
return $c->redirect_to('login') unless $c->session('username');
return 1;
});
});
# a few admin tasks are without authorization
my $admin = $r->any('/admin');
$admin->any('/login')->name('login')->to('admin#')
->tap(get => { template => 'admin/login' })
->tap(post => { action => 'login' });
$admin->any('/logout')->to('admin#logout');
# all others are
$admin = $admin->authorized;
$admin->any('/files')->name('files')
->tap(get => { template => 'admin/files' })
->tap(post => { action => 'upload_files' })
->tap(delete => { action => 'delete_files' });
$r->any('/:name', {name => 1})
->to('page#')
->name('page')
->tap(get => { action => 'get' })
->authorized
->tap(put => { action => 'create' })
->tap(patch => { action => 'update' })
->tap(delete => { action => 'delete' });
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment