BeastBB/lib/BeastBB.pm

90 lines
2.1 KiB
Perl
Raw Normal View History

package BeastBB;
2021-05-24 15:19:04 +02:00
use 5.30.3;
use strict;
use warnings;
use Data::Dumper;
use Mojo::Base 'Mojolicious';
use Mojo::File;
2021-05-24 15:19:04 +02:00
use Const::Fast;
use Params::ValidationCompiler 'validation_for';
2021-05-30 16:56:59 +02:00
use BeastBB::Constants (
'$HOME_DIR', '$HOME_CONFIG_DIR', '$CONFIG_FILE',
'$SECRET_DEFAULT_SIZE'
);
2021-05-24 15:19:04 +02:00
use BeastBB::ConfigWriter;
2021-05-30 16:56:59 +02:00
use BeastBB::Database;
2021-05-24 15:19:04 +02:00
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self;
}
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-05-24 15:19:04 +02:00
sub PrepareSecrets {
my $self = shift;
2021-05-24 15:19:04 +02:00
my $config = $self->config;
$self->secrets( $config->{secrets} );
2021-05-24 15:19:04 +02:00
}
sub PrepareRoutes {
my $self = shift;
my $routes = $self->routes;
@{ $self->renderer->paths() } =
2021-05-24 15:19:04 +02:00
( Mojo::File::curfile->dirname->child('BeastBB')->child('templates')
->to_string );
@{ $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-05-24 15:19:04 +02:00
$routes->get('/')->to('install#welcome');
$routes->post('/install/database')->to('install#install_database');
2021-05-30 16:56:59 +02:00
$routes->post('/install/admin_user_create')
->to('install#admin_user_create');
2021-05-24 15:19:04 +02:00
}
}
2021-05-24 15:19:04 +02:00
sub PrepareConfig {
my $self = shift;
$HOME_CONFIG_DIR->make_path if !-e $HOME_CONFIG_DIR;
if ( !-e $CONFIG_FILE ) {
my $config_writer =
BeastBB::ConfigWriter->new( config_file => $CONFIG_FILE );
$config_writer->PopulateExampleConfigFile(
secret_size => $SECRET_DEFAULT_SIZE );
}
$self->plugin( Config => file => $CONFIG_FILE->to_string );
}
1;