feat: 添加更新功能控制台日志 (console.log/error),标记每步执行与错误输出

This commit is contained in:
2026-05-24 23:40:20 +08:00
parent ee02326d1e
commit f3fcffa9ee
3 changed files with 123 additions and 25 deletions
+16
View File
@@ -1,15 +1,31 @@
import { NextResponse } from "next/server";
import { checkForUpdate } from "@/lib/update";
function apiLog(msg: string) {
const t = new Date().toISOString().slice(0, 19).replace("T", " ");
console.log(`[${t}] [更新检查] ${msg}`);
}
function apiErr(msg: string) {
const t = new Date().toISOString().slice(0, 19).replace("T", " ");
console.error(`[${t}] [更新检查] ${msg}`);
}
// GET /api/update/check - 检查是否有可用更新
export async function GET() {
apiLog("收到版本检查请求");
try {
const versionInfo = await checkForUpdate();
const hasUpdate = versionInfo.hasUpdate;
const latestVer = versionInfo.latest?.version || "无";
apiLog(`检查完成: ${hasUpdate ? "有新版本 " + latestVer : "已是最新"}`);
return NextResponse.json({
success: true,
data: versionInfo,
});
} catch (error) {
const msg = error instanceof Error ? error.message : "未知错误";
apiErr(`检查异常: ${msg}`);
return NextResponse.json(
{ success: false, error: "检查更新失败" },
{ status: 500 }
+17
View File
@@ -1,9 +1,21 @@
const API_TAG = "SSE-API";
function apiLog(msg: string) {
const t = new Date().toISOString().slice(0, 19).replace("T", " ");
console.log(`[${t}] [${API_TAG}] ${msg}`);
}
function apiErr(msg: string) {
const t = new Date().toISOString().slice(0, 19).replace("T", " ");
console.error(`[${t}] [${API_TAG}] ${msg}`);
}
import { executeUpdate } from "@/lib/update";
// POST /api/update/execute - 执行容器更新(SSE 流式输出日志)
export async function POST() {
const encoder = new TextEncoder();
apiLog("收到更新请求,创建 SSE 流");
const stream = new ReadableStream({
async start(controller) {
// 发送日志的辅助函数
@@ -13,8 +25,11 @@ export async function POST() {
};
try {
apiLog("开始执行 executeUpdate...");
const result = await executeUpdate(sendLog);
apiLog(`executeUpdate 完成: success=${result.success}, message="${result.message}"`);
// 发送最终结果
const finalData = JSON.stringify({
done: true,
@@ -25,12 +40,14 @@ export async function POST() {
} catch (error) {
const errMsg =
error instanceof Error ? error.message : "未知错误";
apiErr(`SSE 流异常: ${errMsg}`);
controller.enqueue(
encoder.encode(
`data: ${JSON.stringify({ done: true, success: false, message: errMsg })}\n\n`
)
);
} finally {
apiLog("关闭 SSE 流");
controller.close();
}
},