BeastBB/lib/BeastBB.pm

125 lines
3.0 KiB
Perl

package BeastBB;
use 5.30.3;
use strict;
use warnings;
use Data::Dumper;
use Mojo::Base 'Mojolicious';
use Mojo::File;
use Const::Fast;
use Params::ValidationCompiler 'validation_for';
use BeastBB::Constants (
'HomeConfigDir', 'ConfigFile',
'SecretDefaultSize'
);
use BeastBB::ConfigWriter;
use BeastBB::Database;
use BeastBB::Response;
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self;
}
sub startup {
my $self = shift;
$self->PrepareConfig;
$self->PrepareSecrets;
$self->PrepareRoutes;
$self->PrepareHelpers;
return $self;
}
sub PrepareHelpers {
my $self = shift;
my $database;
$self->helper(
db => sub {
if ( !defined $database ) {
my $config = $self->config;
$database =
BeastBB::Database->NewFromConfig( config => $config->{db} );
}
return $database;
}
);
$self->helper(
LoggedUser => sub {
my $self = shift;
my $user_manager = BeastBB::DAO::UserManager->new( app => $self );
my $session = $self->session;
my $username = $session->{username};
return BeastBB::Response->new(
is_error => 1,
error_message => 'User is not logged in.'
) if !defined $username;
return $user_manager->Get( username => $username );
}
);
}
sub PrepareSecrets {
my $self = shift;
my $config = $self->config;
$self->secrets( $config->{secrets} );
}
sub PrepareRoutes {
my $self = shift;
my $routes = $self->routes;
@{ $self->renderer->paths() } =
( Mojo::File::curfile->dirname->child('BeastBB')->child('templates')
->to_string );
@{ $self->static->paths() } =
( Mojo::File::curfile->dirname->child('BeastBB')->child('public')
->to_string );
print Data::Dumper::Dumper $self->renderer->paths;
if ( !$self->config->{finished_install} ) {
$self->PrepareInstallationRoutes;
return;
}
$self->PrepareCommonRoutes;
}
sub PrepareCommonRoutes {
my $self = shift;
my $routes = $self->routes;
$routes->get('/')->to('Main#Index');
$routes->get('/login')->to('Login#GetLogin');
$routes->post('/login')->to('Login#Login');
$routes->post('/logout')->to('Login#Logout');
}
sub PrepareInstallationRoutes {
my $self = shift;
my $routes = $self->routes;
$routes->get('/')->to('install#welcome');
$routes->post('/install/database')->to('install#install_database');
$routes->post('/install/admin_user_create')
->to('install#admin_user_create');
}
sub PrepareConfig {
my $self = shift;
HomeConfigDir()->make_path if !-e HomeConfigDir();
if ( !-e ConfigFile() ) {
my $config_writer =
BeastBB::ConfigWriter->new( config_file => ConfigFile() );
$config_writer->PopulateExampleConfigFile(
secret_size => SecretDefaultSize() );
}
$self->plugin( Config => file => ConfigFile()->to_string );
}
1;