-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
176 lines (154 loc) · 4.8 KB
/
main.js
File metadata and controls
176 lines (154 loc) · 4.8 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
const fs = require('fs');
const path = require('path');
const https = require('https');
const express = require('express');
const { useAPI } = require("./api")
const { app, BrowserWindow, ipcMain, dialog, shell, Menu } = require('electron');
const PORT = 8080;
// Instruct Electron to ignore certificate errors (development only)
app.commandLine.appendSwitch('ignore-certificate-errors', 'true');
function startServer() {
const server = express();
const staticPath = path.join(__dirname, 'public');
// Basic CORS middleware to allow requests from other local IPs (handles preflight)
server.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type,Authorization');
res.setHeader('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') return res.sendStatus(204);
next();
});
server.use(express.json());
useAPI(server)
// Default route for testing
server.get('/', (req, res) => {
const host = req.headers.host || '';
if (host.includes('localhost')) {
res.sendFile(path.join(__dirname, 'public', 'admin', 'index.html'));
} else {
res.sendFile(path.join(__dirname, 'public', "client", 'index.html'));
}
});
server.use(express.static(staticPath));
// Load your certificate and key (using files generated by mkcert)
const options = {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem')),
};
// Create an HTTPS server
const httpsServer = https.createServer(options, server);
httpsServer.listen(PORT, '0.0.0.0', () => {
console.log(`HTTPS server running at https://0.0.0.0:${PORT}`);
});
}
function getLocalIP() {
const os = require('os');
const addresses = Object.values(os.networkInterfaces())
.flat()
.filter(net => net.family === 'IPv4' && !net.internal)
.map(net => net.address);
console.log('Local IP addresses:', addresses);
return addresses;
}
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
show: false, // Don't show until ready
title: 'XR Engine - Admin Panel',
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'sub.js'),
},
titleBarStyle: 'default', // Show standard title bar with controls
frame: true, // Enable window frame
maximizable: true,
minimizable: true,
closable: true,
resizable: true,
icon: path.join(__dirname, 'assets', 'icon.png') // Optional: add app icon if you have one
});
// Create application menu
const menuTemplate = [
{
label: 'File',
submenu: [
{
label: 'New Project',
accelerator: 'CmdOrCtrl+N',
click: () => {
win.webContents.send('navigate-to-page', 'projects');
}
},
{ type: 'separator' },
{
label: 'Exit',
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
click: () => {
app.quit();
}
}
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'close' }
]
}
];
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
// Show window when ready to prevent visual flash
win.once('ready-to-show', () => {
win.show();
win.maximize(); // Start maximized but still allow window controls
});
// Load the webpage via the HTTPS server
win.loadURL(`https://localhost:${PORT}`);
// Set up IPC handler for server info requests
ipcMain.handle('get-server-info', async () => {
const addresses = getLocalIP();
return {
ip: addresses[0] || 'localhost',
port: PORT,
addresses: addresses
};
});
// Set up IPC handler for file dialog
ipcMain.handle('show-open-dialog', async (event, options) => {
const result = await dialog.showOpenDialog(win, options);
return result;
});
// Set up IPC handler for opening paths in file explorer
ipcMain.handle('open-path', async (event, filePath) => {
await shell.openPath(filePath);
return { success: true };
});
}
app.whenReady().then(() => {
startServer();
createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});