feat: Update database setup and download handling

- Set a fixed database password and ensure old data is overwritten during setup.
- Enhance the download functionality with improved error handling for missing files, providing user-friendly HTML responses.
- Add instructions for placing client builds in the downloads directory in the admin panel.
This commit is contained in:
2026-02-26 21:05:41 +08:00
parent a376a9e0f3
commit 89948f76b7
3 changed files with 39 additions and 11 deletions

View File

@@ -152,6 +152,9 @@ export default function Downloads() {
{building ? '构建中...' : '构建选中平台'}
</button>
</div>
<p className="text-sm text-gray-500 mb-4">
CI FunMC--windows-x64.exe <code className="bg-gray-100 px-1 rounded">downloads</code> DOWNLOADS_DIR /opt/funmc/downloads
</p>
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
{Object.entries(platformInfo).map(([key, info]) => (

View File

@@ -99,19 +99,23 @@ install_nodejs() {
fi
}
# 配置数据库
# 配置数据库(固定密码 12345678安装时覆盖旧数据
setup_database() {
echo -e "${YELLOW}[4/7] 配置数据库...${NC}"
systemctl enable postgresql
systemctl start postgresql
# 生成随机密码
DB_PASSWORD=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | head -c 24)
# 固定数据库密码,安装时覆盖旧库与用户
DB_PASSWORD="12345678"
# 创建数据库和用户
sudo -u postgres psql -c "CREATE USER funmc WITH PASSWORD '$DB_PASSWORD';" 2>/dev/null || true
sudo -u postgres psql -c "CREATE DATABASE funmc OWNER funmc;" 2>/dev/null || true
# 先删除旧数据库和用户(覆盖旧数据)
sudo -u postgres psql -c "DROP DATABASE IF EXISTS funmc;" 2>/dev/null || true
sudo -u postgres psql -c "DROP USER IF EXISTS funmc;" 2>/dev/null || true
# 创建用户和数据库
sudo -u postgres psql -c "CREATE USER funmc WITH PASSWORD '$DB_PASSWORD';"
sudo -u postgres psql -c "CREATE DATABASE funmc OWNER funmc;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE funmc TO funmc;"
# 配置 pg_hba.conf
@@ -122,7 +126,7 @@ setup_database() {
systemctl reload postgresql
fi
echo -e "${GREEN}✓ 数据库配置完成${NC}"
echo -e "${GREEN}✓ 数据库配置完成(密码已设为 12345678已覆盖旧数据${NC}"
echo "$DB_PASSWORD" > /tmp/funmc_db_password
}
@@ -130,8 +134,8 @@ setup_database() {
build_funmc() {
echo -e "${YELLOW}[5/7] 编译 FunMC...${NC}"
# 创建目录
mkdir -p $INSTALL_DIR $CONFIG_DIR $DATA_DIR $LOG_DIR
# 创建目录(含客户端下载目录)
mkdir -p $INSTALL_DIR $INSTALL_DIR/downloads $CONFIG_DIR $DATA_DIR $LOG_DIR
# 克隆或更新代码
if [ -d "$INSTALL_DIR/src" ]; then
@@ -192,6 +196,7 @@ ADMIN_PASSWORD=${ADMIN_PASSWORD}
# 客户端下载
CLIENT_DOWNLOAD_ENABLED=true
CLIENT_VERSION=${FUNMC_VERSION}
DOWNLOADS_DIR=$INSTALL_DIR/downloads
EOF
# 创建中继配置

View File

@@ -256,11 +256,31 @@ pub async fn list_builds() -> Json<Vec<ClientBuild>> {
Json(builds)
}
pub async fn download_file(Path(filename): Path<String>) -> Result<Response, StatusCode> {
pub async fn download_file(Path(filename): Path<String>) -> Result<Response, Response> {
let downloads_dir = std::env::var("DOWNLOADS_DIR").unwrap_or_else(|_| "./downloads".to_string());
let file_path = std::path::Path::new(&downloads_dir).join(&filename);
let file = File::open(&file_path).await.map_err(|_| StatusCode::NOT_FOUND)?;
let file = match File::open(&file_path).await {
Ok(f) => f,
Err(_) => {
let escaped = filename.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;");
let html = format!(r##"<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>文件不存在</title></head>
<body style="font-family:sans-serif;max-width:560px;margin:80px auto;padding:20px;">
<h1>文件不存在</h1>
<p>未找到 <strong>{}</strong>。</p>
<p>可能原因:该版本尚未构建或未上传到服务器。请联系管理员,或将构建好的客户端放入服务器的 <code>downloads</code> 目录后重试。</p>
<p><a href="/download">返回下载页</a></p>
</body>
</html>"##, escaped);
return Err(Response::builder()
.status(StatusCode::NOT_FOUND)
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(Body::from(html))
.unwrap());
}
};
let stream = ReaderStream::new(file);
let body = Body::from_stream(stream);