perf: 优化初始化安装流程,增加mysql和Redis配置校验

This commit is contained in:
xboard 2023-12-18 17:42:59 +08:00
parent 9dcb9cee32
commit 41643a729a

View File

@ -85,6 +85,8 @@ class XboardInstall extends Command
'DB_PASSWORD' => '', 'DB_PASSWORD' => '',
]; ];
}else{ }else{
$isMysqlValid = false;
while(!$isMysqlValid){
$envConfig = [ $envConfig = [
'DB_CONNECTION' => 'mysql', 'DB_CONNECTION' => 'mysql',
'DB_HOST' => text(label: "请输入数据库地址", default: '127.0.0.1', required: true), 'DB_HOST' => text(label: "请输入数据库地址", default: '127.0.0.1', required: true),
@ -93,9 +95,26 @@ class XboardInstall extends Command
'DB_USERNAME' => text(label:'请输入数据库用户名', required: true), 'DB_USERNAME' => text(label:'请输入数据库用户名', required: true),
'DB_PASSWORD' => text(label:'请输入数据库密码', required: false), 'DB_PASSWORD' => text(label:'请输入数据库密码', required: false),
]; ];
try {
\Config::set("database.connections.mysql.host", $envConfig['DB_HOST']);
\Config::set("database.connections.mysql.port", $envConfig['DB_PORT']);
\Config::set("database.connections.mysql.database", $envConfig['DB_DATABASE']);
\Config::set("database.connections.mysql.username", $envConfig['DB_USERNAME']);
\Config::set("database.connections.mysql.password", $envConfig['DB_PASSWORD']);
\DB::purge('mysql');
\DB::connection('mysql')->getPdo();
$isMysqlValid = true;
} catch (\Exception $e) {
// 连接失败,输出错误消息
$this->error("数据库连接失败:" . $e->getMessage());
$this->info("请重新输入数据库配置");
}
}
} }
$envConfig['APP_KEY'] = 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC')); $envConfig['APP_KEY'] = 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC'));
$envConfig['INSTALLED'] = true; $envConfig['INSTALLED'] = true;
$isReidsValid = false;
while(!$isReidsValid){
// 判断是否为Docker环境 // 判断是否为Docker环境
if ($isDocker == 'true' && (confirm(label: '是否启用Docker内置的Redis', default: true, yes:'启用', no:'不启用'))){ if ($isDocker == 'true' && (confirm(label: '是否启用Docker内置的Redis', default: true, yes:'启用', no:'不启用'))){
$envConfig['REDIS_HOST'] = '/run/redis-socket/redis.sock'; $envConfig['REDIS_HOST'] = '/run/redis-socket/redis.sock';
@ -106,6 +125,25 @@ class XboardInstall extends Command
$envConfig['REDIS_PORT'] = text(label: '请输入Redis端口', default: '6379', required: true); $envConfig['REDIS_PORT'] = text(label: '请输入Redis端口', default: '6379', required: true);
$envConfig['REDIS_PASSWORD'] = text(label: '请输入redis密码(默认: null)', default: ''); $envConfig['REDIS_PASSWORD'] = text(label: '请输入redis密码(默认: null)', default: '');
} }
$redisConfig = [
'client' => 'phpredis', // 或 'phpredis'
'default' => [
'host' => $envConfig['REDIS_HOST'],
'password' => $envConfig['REDIS_PASSWORD'],
'port' => $envConfig['REDIS_PORT'],
'database' => 0,
],
];
try{
$redis = new \Illuminate\Redis\RedisManager(app(), 'phpredis', $redisConfig);
$redis->ping();
$isReidsValid = true;
}catch(\Exception $e){
// 连接失败,输出错误消息
$this->error("redis连接失败" . $e->getMessage());
$this->info("请重新输入REDIS配置");
}
}
if (!copy(base_path() . '/.env.example', base_path() . '/.env')) { if (!copy(base_path() . '/.env.example', base_path() . '/.env')) {
abort(500, '复制环境文件失败,请检查目录权限'); abort(500, '复制环境文件失败,请检查目录权限');