-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
193 lines (163 loc) · 5.76 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { app, BrowserWindow } = require("electron");
const { spawn } = require("child_process");
const path = require("path");
const axios = require("axios");
const fs = require("fs");
class PythonEnvironment {
constructor() {
this.isWindows = process.platform === "win32";
// 修改为新的路径
this.envPath = path.join(__dirname, "resources", process.platform === "darwin" ? "macOS_arm64" : "Windows_x64");
this.flaskProcess = null;
// 设置 Python 解释器路径
if (this.isWindows) {
this.pythonPath = path.join(this.envPath, "python.exe");
this.altPythonPaths = [
path.join(this.envPath, "Scripts", "python.exe"),
path.join(this.envPath, "bin", "python.exe"),
];
} else {
// MacOS 路径
this.pythonPath = path.join(this.envPath, "bin", "python3.10");
this.altPythonPaths = [
path.join(this.envPath, "bin", "python3"),
path.join(this.envPath, "bin", "python"),
];
}
}
findPythonExecutable() {
// 检查主路径
if (fs.existsSync(this.pythonPath)) {
return this.pythonPath;
}
// 检查备选路径
for (const altPath of this.altPythonPaths) {
if (fs.existsSync(altPath)) {
this.pythonPath = altPath;
return altPath;
}
}
throw new Error("Python executable not found in: " + this.envPath);
}
async initialize() {
// 确保 python_env 目录存在
if (!fs.existsSync(this.envPath)) {
throw new Error("Python environment not found at: " + this.envPath);
}
this.findPythonExecutable();
// 设置环境变量
if (this.isWindows) {
const scriptsPath = path.join(this.envPath, "Scripts");
const binPath = path.join(this.envPath, "bin");
process.env.PATH = `${scriptsPath};${binPath};${process.env.PATH}`;
} else {
// MacOS 特定配置
process.env.PATH = `${path.join(this.envPath, "bin")}:${process.env.PATH}`;
process.env.DYLD_LIBRARY_PATH = path.join(this.envPath, "lib");
}
process.env.PYTHONHOME = this.envPath;
process.env.PYTHONPATH = this.envPath;
}
startFlaskServer() {
return new Promise((resolve, reject) => {
console.log("Starting Flask server...");
// Flask 应用路径
const flaskAppPath = path.join(__dirname, "stellarcore", "app.py");
if (!fs.existsSync(flaskAppPath)) {
reject(new Error(`Flask app not found at: ${flaskAppPath}`));
return;
}
const options = {
stdio: "inherit",
cwd: __dirname,
env: process.env
};
if (!this.isWindows) {
options.chmod = 0o755;
}
console.log(`Starting Flask with Python: ${this.pythonPath}`);
console.log(`Flask app path: ${flaskAppPath}`);
this.flaskProcess = spawn(this.pythonPath, [flaskAppPath], options);
this.flaskProcess.on("error", (err) => {
console.error("Failed to start Flask process:", err);
reject(err);
});
// 健康检查
const checkFlask = setInterval(async () => {
try {
const response = await axios.get("http://127.0.0.1:5001/health");
if (response.status === 200 && response.data.status === "healthy") {
clearInterval(checkFlask);
console.log("Flask server is running.");
resolve();
}
} catch (error) {
console.log("Waiting for Flask server to start...");
}
}, 500);
// 30秒超时
setTimeout(() => {
clearInterval(checkFlask);
reject(new Error("Flask server startup timeout after 30 seconds"));
}, 30000);
});
}
cleanup() {
if (this.flaskProcess) {
console.log("Shutting down Flask server...");
this.flaskProcess.kill();
this.flaskProcess = null;
}
}
}
let mainWindow;
let pythonEnv = new PythonEnvironment();
const createMainWindow = () => {
mainWindow = new BrowserWindow({
width: 1200, // 调整窗口大小
height: 800,
webPreferences: {
contextIsolation: true,
},
});
// 加载 React 构建文件
const reactBuildPath = path.join(__dirname, "nebulaview", "build", "index.html");
mainWindow.loadFile(reactBuildPath);
if (process.env.NODE_ENV === "development") {
mainWindow.webContents.openDevTools();
}
mainWindow.on("closed", () => {
mainWindow = null;
});
};
app.on("ready", async () => {
try {
console.log("Initializing Python environment...");
await pythonEnv.initialize();
console.log("Starting Flask application...");
await pythonEnv.startFlaskServer();
console.log("Creating Electron window...");
createMainWindow();
} catch (error) {
console.error("Application startup failed:", error);
app.quit();
}
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow === null) {
createMainWindow();
}
});
app.on("quit", () => {
pythonEnv.cleanup();
});
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
pythonEnv.cleanup();
app.quit();
});