package MJB::Web; use Mojo::Base 'Mojolicious', -signatures; use MJB::DB; use MJB::Backend::Jekyll; 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 = MJB::DB->connect($config->{database}->{mjb}); }); $self->helper( jekyll => sub ($c, $domain) { return MJB::Backend::Jekyll->new( root => '/home/manager/mjb/Web/repos', # Make a config option. domain => $domain, init_from => 'https://store.myjekyllblog.com/symkat/symkat.jekyllblog.com.git', repo => 'git@store.myjekyllblog.com:mjb/' . $domain . '.git', ); }); # Minion plugin & tasks $self->plugin( Minion => { Pg => $self->config->{database}->{minion} } ); # Blog deployment related jobs. $self->minion->add_task( create_blog => 'MJB::Web::Task::CreateBlog' ); $self->minion->add_task( purge_blog => 'MJB::Web::Task::PurgeBlog' ); $self->minion->add_task( deploy_blog => 'MJB::Web::Task::DeployBlog' ); # SSL cert related jobs. $self->minion->add_task( create_ssl_cert => 'MJB::Web::Task::CreateSSLCert' ); $self->minion->add_task( sync_ssl_certs => 'MJB::Web::Task::SyncSSLCerts' ); # 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; }); # Minion Admin Panel $self->plugin( 'Minion::Admin' => { route => $admin->under('/minion' => sub ($c) { return 1; } ), }); # 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' ); # User registration, login, and logout. $r->get ( '/register' )->to( 'Auth#register' )->name('show_register' ); $r->post ( '/register' )->to( 'Auth#do_register' )->name('do_register' ); $r->get ( '/login' )->to( 'Auth#login' )->name('show_login' ); $r->post ( '/login' )->to( 'Auth#do_login' )->name('do_login' ); $auth->get( '/logout' )->to( 'Auth#do_logout' )->name('do_logout' ); # User Forgot Password Workflow. $r->get ( '/forgot' )->to('Auth#forgot' )->name('show_forgot' ); $r->post( '/forgot' )->to('Auth#do_forgot' )->name('do_forgot' ); $r->get ( '/reset/:token' )->to('Auth#reset' )->name('show_reset' ); $r->post( '/reset/:token' )->to('Auth#do_reset' )->name('do_reset' ); # User setting changes when logged in $auth->get ( '/profile' )->to('UserSettings#profile' )->name('show_profile' ); $auth->post( '/profile' )->to('UserSettings#do_profile' )->name('do_profile' ); $auth->get ( '/password' )->to('UserSettings#change_password' )->name('show_change_password' ); $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); # Dashboard $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); $auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); # Blog Management $auth->get ( '/blog/create' )->to('Blog#create' )->name('show_blog_create' ); $auth->post( '/blog/create' )->to('Blog#do_create' )->name('do_blog_create' ); } 1;