Commit bfbcdef1 authored by wolfocde's avatar wolfocde

update

parent 02a970cb
......@@ -2,7 +2,7 @@ APP_DEBUG = true
DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_DATABASE = my_blog
DB_DATABASE = my_blog_2022
DB_USERNAME = root
DB_PASSWORD = root
DB_CHARSET = utf8mb4
\ No newline at end of file
......@@ -8,16 +8,36 @@ use Webman\Http\Request;
/**
* Class InstallCheck 博客程序首次安装检测
*
* @package app\middleware
*/
class InstallCheck implements MiddlewareInterface
{
public function process(Request $request, callable $next): Response
public function process(Request $request, callable $handler): Response
{
$lock_file = base_path() . DIRECTORY_SEPARATOR . 'extend' . DIRECTORY_SEPARATOR . 'install.lock';
if (!file_exists($lock_file)) {
$dbHost = env('db_host');
$dbUser = env('db_username');
$dbPwd = env('db_password');
$dbPort = env('db_port');
$dbCharset = env('db_charset');
$dbName = env('db_database');
try {
$conn = mysqli_connect($dbHost, $dbUser, $dbPwd, null, $dbPort);
mysqli_query($conn, "SET NAMES {$dbCharset}");
if (!mysqli_select_db($conn, $dbName)) {
if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET {$dbCharset};")) {
$errorMsg = "数据库{$dbName} 不存在,也没权限创建新的数据库!";
mysqli_close($conn);
return response($errorMsg, 400);
}
}
} catch (\Exception $e) {
$errorMsg = "连接 MySQL 失败: " . mysqli_connect_error() . $e->getMessage();
return response($errorMsg, 400);
}
}
return $next($request);
return $handler($request);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment