-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
238 lines (209 loc) · 6.61 KB
/
main.js
File metadata and controls
238 lines (209 loc) · 6.61 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const { app, BrowserWindow, ipcMain, safeStorage, shell, dialog } = require('electron');
const path = require('path');
const { spawn, execSync } = require('child_process');
const fs = require('fs');
let backendProcess = null;
// Path to store encrypted credentials
const credentialsPath = path.join(app.getPath('userData'), 'credentials.enc');
const aiKeyPath = path.join(app.getPath('userData'), 'ai-key.enc');
function startBackend() {
const backendDir = path.join(__dirname, 'backend');
const binaryName = process.platform === 'win32' ? 'auxa-server.exe' : 'auxa-server';
const binaryPath = path.join(backendDir, binaryName);
ensurePortFree(3000);
const launchProcess = (command, args) => {
console.log(`Starting backend server with "${command} ${args.join(' ')}"`);
backendProcess = spawn(command, args, {
cwd: backendDir
});
backendProcess.stdout.on('data', (data) => {
console.log(`Backend: ${data}`);
});
backendProcess.stderr.on('data', (data) => {
console.error(`Backend Error: ${data}`);
});
backendProcess.on('error', (error) => {
console.error('Backend failed to start:', error);
dialog.showErrorBox('Backend Failed', `Failed to start backend process:\n\n${error.message}`);
});
backendProcess.on('close', (code) => {
console.log(`Backend process exited with code ${code}`);
});
};
if (fs.existsSync(binaryPath)) {
launchProcess(binaryPath, []);
return;
}
if (!app.isPackaged) {
console.warn(`Backend binary not found at ${binaryPath}. Falling back to "go run main.go" for development.`);
launchProcess('go', ['run', 'main.go']);
return;
}
const message = `Auxa backend binary was not found at:\n${binaryPath}\n\n` +
'Run "npm run build:backend" before starting the app.';
console.error(message);
dialog.showErrorBox('Backend Missing', message);
}
function ensurePortFree(port) {
try {
if (process.platform === 'win32') {
const output = execSync(`netstat -ano | findstr :${port}`, { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
const pids = new Set();
output.split('\n').forEach(line => {
const parts = line.trim().split(/\s+/);
if (parts.length > 0) {
const pid = parts[parts.length - 1];
if (pid && /^\d+$/.test(pid)) {
pids.add(pid);
}
}
});
pids.forEach(pid => {
try {
execSync(`taskkill /PID ${pid} /F`);
console.log(`Freed port ${port} by killing PID ${pid}`);
} catch (error) {
console.warn(`Failed to kill PID ${pid}: ${error.message}`);
}
});
} else {
const output = execSync(`lsof -ti :${port}`, { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
output.split('\n').filter(Boolean).forEach(pid => {
try {
execSync(`kill -9 ${pid}`);
console.log(`Freed port ${port} by killing PID ${pid}`);
} catch (error) {
console.warn(`Failed to kill PID ${pid}: ${error.message}`);
}
});
}
} catch (error) {
if (error.status === 1) {
// No process was using the port
return;
}
console.warn(`Unable to inspect port ${port}: ${error.message}`);
}
}
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 700,
minWidth: 600,
minHeight: 500,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
},
backgroundColor: '#f5f5f5',
titleBarStyle: 'default',
show: false
});
mainWindow.loadFile('index.html');
// Open external links in the system's default browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
// Open in external browser (Chrome, Safari, etc.)
shell.openExternal(url);
return { action: 'deny' }; // Prevent opening in Electron
});
// Show window when ready to avoid visual flash
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// Open DevTools in development (optional)
// mainWindow.webContents.openDevTools();
}
// Secure storage handlers using safeStorage API
ipcMain.handle('save-credentials', async (event, token, school) => {
try {
const data = JSON.stringify({ token, school });
const encrypted = safeStorage.encryptString(data);
fs.writeFileSync(credentialsPath, encrypted);
return { success: true };
} catch (error) {
console.error('Error saving credentials:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('get-credentials', async () => {
try {
if (!fs.existsSync(credentialsPath)) {
return null;
}
const encrypted = fs.readFileSync(credentialsPath);
const decrypted = safeStorage.decryptString(encrypted);
return JSON.parse(decrypted);
} catch (error) {
console.error('Error reading credentials:', error);
return null;
}
});
ipcMain.handle('delete-credentials', async () => {
try {
if (fs.existsSync(credentialsPath)) {
fs.unlinkSync(credentialsPath);
}
return { success: true };
} catch (error) {
console.error('Error deleting credentials:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('save-ai-key', async (event, apiKey) => {
try {
const encrypted = safeStorage.encryptString(apiKey);
fs.writeFileSync(aiKeyPath, encrypted);
return { success: true };
} catch (error) {
console.error('Error saving AI key:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('get-ai-key', async () => {
try {
if (!fs.existsSync(aiKeyPath)) {
return null;
}
const encrypted = fs.readFileSync(aiKeyPath);
return safeStorage.decryptString(encrypted);
} catch (error) {
console.error('Error reading AI key:', error);
return null;
}
});
ipcMain.handle('delete-ai-key', async () => {
try {
if (fs.existsSync(aiKeyPath)) {
fs.unlinkSync(aiKeyPath);
}
return { success: true };
} catch (error) {
console.error('Error deleting AI key:', error);
return { success: false, error: error.message };
}
});
app.whenReady().then(() => {
// Start backend server
startBackend();
// Wait a moment for backend to start, then create window
setTimeout(() => {
createWindow();
}, 1000);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('quit', () => {
// Kill backend process when app quits
if (backendProcess) {
backendProcess.kill();
}
});