chore: 初始化提交

This commit is contained in:
tongtong committed 2026-09-21 13:01:45 +08:00
commit 943c0a0e5a
71 files changed
+7122

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
const { app, BrowserWindow, shell } = require("electron");
const fs = require("node:fs");
const http = require("node:http");
const path = require("node:path");
const API_TARGET = process.env.CANVAS_API_TARGET || "http://127.0.0.1:8080";
const DEV_URL = process.env.CANVAS_DESKTOP_DEV_URL || "";
const MIME_TYPES = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".webp": "image/webp",
".ico": "image/x-icon",
".woff": "font/woff",
".woff2": "font/woff2",
".wasm": "application/wasm",
".mp4": "video/mp4",
};
/**
* 渲染层通过本地 HTTP 提供,而不是 file://:
* 这样 SPA 路由(history 模式)与相对资源路径都能正常工作。
* 同时把 /api 反代到本机后端服务。
*/
function createServer(root) {
return http.createServer((req, res) => {
if (req.url && req.url.startsWith("/api/")) {
proxyToBackend(req, res);
return;
}
serveStaticFile(req, res, root);
});
}
function proxyToBackend(req, res) {
const target = new URL(req.url, API_TARGET);
const upstream = http.request(
{
hostname: target.hostname,
port: target.port,
path: target.pathname + target.search,
method: req.method,
headers: { ...req.headers, host: target.host },
},
(upstreamRes) => {
res.writeHead(upstreamRes.statusCode || 502, upstreamRes.headers);
upstreamRes.pipe(res);
},
);
upstream.on("error", () => {
if (res.headersSent) return;
res.writeHead(502, { "Content-Type": "application/json; charset=utf-8" });
res.end(JSON.stringify({ error: "后端服务未启动,请先启动 backend" }));
});
req.pipe(upstream);
}
function serveStaticFile(req, res, root) {
const requestPath = decodeURIComponent((req.url || "/").split("?")[0]);
let filePath = path.join(root, requestPath);
if (!filePath.startsWith(root)) {
res.writeHead(403).end();
return;
}
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
// SPA 回退
filePath = path.join(root, "index.html");
}
const contentType = MIME_TYPES[path.extname(filePath).toLowerCase()] || "application/octet-stream";
res.writeHead(200, { "Content-Type": contentType });
fs.createReadStream(filePath).pipe(res);
}
function createWindow(url) {
const win = new BrowserWindow({
width: 1440,
height: 900,
minWidth: 1024,
minHeight: 640,
backgroundColor: "#fafafa",
title: "无限画布",
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
},
});
win.loadURL(url);
// 外部链接交给系统浏览器,避免在应用内打开
win.webContents.setWindowOpenHandler(({ url: target }) => {
shell.openExternal(target);
return { action: "deny" };
});
}
app.whenReady().then(() => {
if (DEV_URL) {
createWindow(DEV_URL);
} else {
const root = path.join(__dirname, "dist");
if (!fs.existsSync(path.join(root, "index.html"))) {
throw new Error("缺少 dist/index.html,请先执行 pnpm build");
}
const server = createServer(root);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
createWindow(`http://127.0.0.1:${address.port}`);
});
}
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow(DEV_URL || "about:blank");
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});