91 lines
2.5 KiB
Perl
91 lines
2.5 KiB
Perl
package BeastBB::Controller::Install;
|
|
|
|
use 5.30.3;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
use Mojo::URL;
|
|
|
|
use Params::ValidationCompiler 'validation_for';
|
|
use Types::Standard qw/HashRef/;
|
|
|
|
use BeastBB::Database;
|
|
use BeastBB::ConfigWriter;
|
|
use BeastBB::Constants ('$CONFIG_FILE');
|
|
|
|
sub welcome {
|
|
my $self = shift;
|
|
my $error = $self->param('error');
|
|
$self->stash( error => $error );
|
|
$self->stash( config => $self->config );
|
|
$self->render;
|
|
}
|
|
|
|
sub install_database {
|
|
my $self = shift;
|
|
|
|
if ( exists $self->config->{db} ) {
|
|
}
|
|
my $config_db;
|
|
my $user = $self->param('username');
|
|
my $password = $self->param('password');
|
|
my $host = $self->param('host');
|
|
my $port = $self->param('port');
|
|
my $dbname = $self->param('dbname');
|
|
|
|
$user = undef if ( length $user ) == 0;
|
|
$password = undef if ( length $password ) == 0;
|
|
$host = undef if ( length $host ) == 0;
|
|
$port = undef if ( length $port ) == 0;
|
|
$dbname = undef if ( length $dbname ) == 0;
|
|
|
|
$config_db = {
|
|
( defined $user ) ? ( user => $user ) : (),
|
|
( defined $password ) ? ( password => $password ) : (),
|
|
( defined $host ) ? ( host => $host ) : (),
|
|
( defined $port ) ? ( port => $port ) : (),
|
|
( defined $dbname ) ? ( dbname => $dbname ) : (),
|
|
};
|
|
|
|
eval { $self->TestNewDatabaseConfig( config => $config_db ); };
|
|
|
|
if ($@) {
|
|
STDERR->say($@);
|
|
my $error_url = Mojo::URL->new('/')->query( error => $@ );
|
|
$self->redirect_to($error_url);
|
|
}
|
|
$self->config->{db} = $config_db;
|
|
local $Data::Dumper::Terse = 1;
|
|
my $config_writer =
|
|
BeastBB::ConfigWriter->new( config_file => $CONFIG_FILE );
|
|
$config_writer->WriteToConfigFile( content => Data::Dumper::Dumper $self->config );
|
|
$self->redirect_to('/');
|
|
}
|
|
|
|
{
|
|
my $validator =
|
|
validation_for( params => { config => { type => HashRef } } );
|
|
|
|
sub TestNewDatabaseConfig {
|
|
my $self = shift;
|
|
my %params = $validator->(@_);
|
|
my $config = $params{config};
|
|
my $database = BeastBB::Database->NewFromConfig( config => $config );
|
|
my $succesful_query_result_array =
|
|
$database->DB->query('SELECT 1')->array;
|
|
if (
|
|
!(
|
|
exists $succesful_query_result_array->[0]
|
|
&& $succesful_query_result_array->[0] == 1
|
|
)
|
|
)
|
|
{
|
|
die "Unable to retrieve data from the database.";
|
|
}
|
|
|
|
}
|
|
}
|
|
1;
|