2021-04-17 20:05:40 +02:00
|
|
|
package BeastBB;
|
|
|
|
|
2021-05-24 15:19:04 +02:00
|
|
|
use 5.30.3;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Data::Dumper;
|
|
|
|
|
2021-04-17 20:05:40 +02:00
|
|
|
use Mojo::Base 'Mojolicious';
|
|
|
|
use Mojo::File;
|
2021-05-24 15:19:04 +02:00
|
|
|
|
|
|
|
use Const::Fast;
|
|
|
|
use Params::ValidationCompiler 'validation_for';
|
|
|
|
|
2021-06-06 00:19:57 +02:00
|
|
|
use BeastBB::Constants (
|
|
|
|
'HomeConfigDir', 'ConfigFile',
|
|
|
|
'SecretDefaultSize'
|
|
|
|
);
|
2021-05-24 15:19:04 +02:00
|
|
|
use BeastBB::ConfigWriter;
|
2021-05-30 16:56:59 +02:00
|
|
|
use BeastBB::Database;
|
2021-06-03 23:44:00 +02:00
|
|
|
use BeastBB::Response;
|
2021-05-24 15:19:04 +02:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my $self = $class->SUPER::new(@_);
|
|
|
|
$self;
|
|
|
|
}
|
2021-04-17 20:05:40 +02:00
|
|
|
|
|
|
|
sub startup {
|
2021-05-24 15:19:04 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->PrepareConfig;
|
|
|
|
$self->PrepareSecrets;
|
|
|
|
$self->PrepareRoutes;
|
2021-05-30 16:56:59 +02:00
|
|
|
$self->PrepareHelpers;
|
2021-05-24 15:19:04 +02:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:56:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
);
|
2021-06-03 23:44:00 +02:00
|
|
|
$self->helper(
|
2021-06-06 00:19:57 +02:00
|
|
|
LoggedUser => sub {
|
|
|
|
my $self = shift;
|
|
|
|
my $user_manager = BeastBB::DAO::UserManager->new( app => $self );
|
|
|
|
my $session = $self->session;
|
2021-06-03 23:44:00 +02:00
|
|
|
my $username = $session->{username};
|
2021-06-06 00:19:57 +02:00
|
|
|
return BeastBB::Response->new(
|
|
|
|
is_error => 1,
|
|
|
|
error_message => 'User is not logged in.'
|
|
|
|
) if !defined $username;
|
|
|
|
return $user_manager->Get( username => $username );
|
2021-06-03 23:44:00 +02:00
|
|
|
}
|
|
|
|
);
|
2021-05-30 16:56:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-24 15:19:04 +02:00
|
|
|
sub PrepareSecrets {
|
2021-04-17 20:05:40 +02:00
|
|
|
my $self = shift;
|
2021-05-24 15:19:04 +02:00
|
|
|
my $config = $self->config;
|
2021-04-17 20:05:40 +02:00
|
|
|
$self->secrets( $config->{secrets} );
|
2021-05-24 15:19:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub PrepareRoutes {
|
|
|
|
my $self = shift;
|
|
|
|
my $routes = $self->routes;
|
2021-06-03 23:44:00 +02:00
|
|
|
|
2021-04-17 20:05:40 +02:00
|
|
|
@{ $self->renderer->paths() } =
|
2021-05-24 15:19:04 +02:00
|
|
|
( Mojo::File::curfile->dirname->child('BeastBB')->child('templates')
|
|
|
|
->to_string );
|
2021-04-17 20:05:40 +02:00
|
|
|
@{ $self->static->paths() } =
|
2021-05-24 15:19:04 +02:00
|
|
|
( Mojo::File::curfile->dirname->child('BeastBB')->child('public')
|
|
|
|
->to_string );
|
|
|
|
print Data::Dumper::Dumper $self->renderer->paths;
|
2021-05-30 16:56:59 +02:00
|
|
|
if ( !exists $self->config->{finished_install} ) {
|
2021-06-03 23:44:00 +02:00
|
|
|
$self->PrepareInstallationRoutes;
|
|
|
|
return;
|
2021-05-24 15:19:04 +02:00
|
|
|
}
|
2021-06-03 23:44:00 +02:00
|
|
|
|
|
|
|
$self->PrepareCommonRoutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub PrepareCommonRoutes {
|
|
|
|
my $self = shift;
|
|
|
|
my $routes = $self->routes;
|
|
|
|
|
|
|
|
$routes->get('/')->to('Main#Index');
|
2021-06-06 00:19:57 +02:00
|
|
|
$routes->get('/login')->to('Login#GetLogin');
|
|
|
|
$routes->post('/login')->to('Login#Login');
|
2021-06-06 02:22:19 +02:00
|
|
|
$routes->post('/logout')->to('Login#Logout');
|
2021-06-03 23:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2021-04-17 20:05:40 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 15:19:04 +02:00
|
|
|
sub PrepareConfig {
|
|
|
|
my $self = shift;
|
2021-06-05 15:25:02 +02:00
|
|
|
HomeConfigDir()->make_path if !-e HomeConfigDir();
|
|
|
|
if ( !-e ConfigFile() ) {
|
2021-05-24 15:19:04 +02:00
|
|
|
my $config_writer =
|
2021-06-05 15:25:02 +02:00
|
|
|
BeastBB::ConfigWriter->new( config_file => ConfigFile() );
|
2021-05-24 15:19:04 +02:00
|
|
|
$config_writer->PopulateExampleConfigFile(
|
2021-06-05 15:25:02 +02:00
|
|
|
secret_size => SecretDefaultSize() );
|
2021-05-24 15:19:04 +02:00
|
|
|
}
|
2021-06-05 15:25:02 +02:00
|
|
|
$self->plugin( Config => file => ConfigFile()->to_string );
|
2021-05-24 15:19:04 +02:00
|
|
|
}
|
2021-04-17 20:05:40 +02:00
|
|
|
1;
|