package MJB::Web; use Mojo::Base 'Mojolicious', -signatures; use MJB::DB; sub startup ($self) { my $config = $self->plugin('NotYAMLConfig', { file => -e 'mjb.yml' ? 'mjb.yml' : '/etc/mjb.yml' }); # Configure the application $self->secrets($config->{secrets}); # Set the cookie expires to 30 days. $self->sessions->default_expiration(2592000); # Load our custom commands. push @{$self->commands->namespaces}, 'MJB::Web::Command'; $self->helper( db => sub { return state $db = WeightGrapher::DB->connect($config->{database}->{mjb}); }); # Standard router. my $r = $self->routes->under( '/' => sub ($c) { # If the user has a uid session cookie, then load their user account. if ( $c->session('uid') ) { my $person = $c->db->resultset('Person')->find( $c->session('uid') ); if ( $person && $person->is_enabled ) { $c->stash->{person} = $person; } } return 1; }); # Create a router chain that ensures the request is from an authenticated user. my $auth = $r->under( '/' => sub ($c) { # Logged in user exists. if ( $c->stash->{person} ) { return 1; } # No user account for this seession. $c->redirect_to( $c->url_for( 'show_login' ) ); return undef; }); # Create a router chain that ensures the request is from an admin user. my $admin = $auth->under( '/' => sub ($c) { # Logged in user exists. if ( $c->stash->{person}->is_admin ) { return 1; } # No user account for this seession. $c->redirect_to( $c->url_for( 'show_dashboard' ) ); return undef; }); # General Informational Pages $r->get ( '/' )->to( 'Root#index' )->name('show_homepage' ); $r->get ( '/about' )->to( 'Root#about' )->name('show_about' ); $r->get ( '/pricing' )->to( 'Root#pricing' )->name('show_pricing' ); $r->get ( '/contact' )->to( 'Root#contact' )->name('show_contact' ); } 1;