-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
163 lines (139 loc) · 3.81 KB
/
Copy pathmain.js
File metadata and controls
163 lines (139 loc) · 3.81 KB
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
const { app, BrowserWindow } = require('electron');
const path = require('path');
const { spawn, exec } = require('child_process');
const http = require('http');
let mainWindow = null;
let splashWindow = null;
let backendProcess = null;
// Determine if we are running in development mode
const isDev = !app.isPackaged && process.argv.includes('--dev');
// 1. Spawning FastAPI Backend
function startBackend() {
let pythonPath;
let workingDir;
if (isDev) {
// Development environment
pythonPath = path.join(__dirname, 'env', 'Scripts', 'python.exe');
workingDir = __dirname;
} else {
// Packaged Electron app environment
pythonPath = path.join(process.resourcesPath, 'env', 'Scripts', 'python.exe');
workingDir = process.resourcesPath;
}
console.log(`Starting FastAPI Backend via: ${pythonPath}`);
console.log(`Working Directory: ${workingDir}`);
// Spawn python relatively and call uvicorn as a module to avoid shebang path bugs
backendProcess = spawn(pythonPath, ['-m', 'uvicorn', 'app:app', '--host', '127.0.0.1', '--port', '8000'], {
cwd: workingDir,
env: { ...process.env }
});
backendProcess.stdout.on('data', (data) => {
console.log(`[FastAPI Stdout]: ${data}`);
});
backendProcess.stderr.on('data', (data) => {
console.error(`[FastAPI Stderr]: ${data}`);
});
backendProcess.on('close', (code) => {
console.log(`FastAPI Backend process exited with code ${code}`);
});
}
// 2. Process Cleanup
function killBackendProcess() {
if (backendProcess) {
console.log(`Terminating FastAPI process tree (PID: ${backendProcess.pid})...`);
// Windows-specific process tree taskkill
exec(`taskkill /pid ${backendProcess.pid} /t /f`, (err) => {
if (err) {
console.error('Failed taskkill, running childProcess.kill():', err);
backendProcess.kill();
}
backendProcess = null;
});
}
}
// 3. Create Windows
function createSplashWindow() {
splashWindow = new BrowserWindow({
width: 480,
height: 280,
transparent: true,
frame: false,
alwaysOnTop: true,
resizable: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true
}
});
splashWindow.loadFile('splash.html');
splashWindow.on('closed', () => {
splashWindow = null;
});
}
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 1300,
height: 850,
show: false, // Shown once ready
backgroundColor: '#080c14',
webPreferences: {
nodeIntegration: false,
contextIsolation: true
}
});
if (isDev) {
mainWindow.loadURL('http://localhost:3000');
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(path.join(__dirname, 'frontend', 'dist', 'index.html'));
}
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// 4. Ping Backend Polling Loop
function checkBackendOnline() {
const req = http.request({
host: '127.0.0.1',
port: 8000,
path: '/timeline',
method: 'GET',
timeout: 1000
}, (res) => {
// Backend is ready! Close splash and open main window
createMainWindow();
if (splashWindow) {
splashWindow.close();
splashWindow = null;
}
});
req.on('error', () => {
// Retry in 500ms
setTimeout(checkBackendOnline, 500);
});
req.end();
}
// 5. Electron Application Lifecycle
app.whenReady().then(() => {
createSplashWindow();
startBackend();
checkBackendOnline();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createSplashWindow();
checkBackendOnline();
}
});
});
app.on('window-all-closed', () => {
killBackendProcess();
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('will-quit', () => {
killBackendProcess();
});