IYUUPlus-Windows/server/server.js

114 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2024-06-28 18:37:00 +08:00
const { spawn, exec, execFile } = require('child_process');
2024-06-27 17:29:25 +08:00
const os = require('os');
const path = require('path');
2024-06-28 16:13:57 +08:00
const log = require('electron-log')
2024-07-01 14:08:06 +08:00
const mainWin = require('../windows/app')
let serverProcess = null;
2024-06-27 17:29:25 +08:00
2024-06-28 21:57:14 +08:00
function getPhpPath(resourcePath) {
2024-06-27 17:29:25 +08:00
const arch = os.arch();
if (arch === 'x64') {
2024-06-28 21:57:14 +08:00
return path.join(resourcePath, 'run', 'php-8.3.8-x64');
2024-06-27 17:29:25 +08:00
} else if (arch === 'ia32') {
2024-06-28 21:57:14 +08:00
return path.join(resourcePath, 'run', 'php-8.3.8-x86');
2024-06-27 17:29:25 +08:00
} else {
2024-06-28 16:13:57 +08:00
log.info("Unsupported architecture:", arch);
2024-06-27 17:29:25 +08:00
process.exit(1);
}
}
function startServer() {
2024-07-01 14:08:06 +08:00
if (!serverProcess) {
let resourcePath = process.resourcesPath;
if (resourcePath.includes('node_modules')) {
resourcePath = process.cwd();
}
2024-06-28 12:21:47 +08:00
2024-07-01 14:08:06 +08:00
const env = { ...process.env };
const phpDir = getPhpPath(resourcePath);
env.Path = `${phpDir};${env.Path}`;
const workingDir = path.resolve(path.join(resourcePath, 'iyuu'));
log.info(`[IYUU] 工作目录: ${workingDir}`);
serverProcess = execFile('cmd', ['/c', 'windows.bat'], {
cwd: workingDir,
env: env,
killSignal: 'SIGTERM',
windowsHide: true,
}, (error, stdout, stderr) => {
if (error) {
log.error(`[IYUU] 服务启动错误: ${error.message}`);
} else {
log.error(`[IYUU] 服务启动 stderr: ${stderr}`);
log.info(`[IYUU] 服务启动成功: ${stdout}`);
}
2024-06-28 21:57:14 +08:00
});
2024-06-28 18:37:00 +08:00
2024-07-01 14:08:06 +08:00
// serverProcess = spawn('cmd', ['/c', 'windows.bat'], {
// cwd: workingDir,
// stdio: 'pipe',
// env: env,
// detached: true,
// windowsHide: true
// });
2024-06-28 21:57:14 +08:00
2024-07-01 14:08:06 +08:00
serverProcess.unref();
2024-06-28 21:57:14 +08:00
2024-07-01 14:08:06 +08:00
if (serverProcess.stdout) {
serverProcess.stdout.on('data', (data) => {
log.info(`[IYUU] ${data}`);
});
serverProcess.stderr.on('data', (data) => {
log.error(`[IYUU] ${data}`);
});
}
2024-06-27 17:29:25 +08:00
2024-07-01 14:08:06 +08:00
serverProcess.on('error', (err) => {
log.error(`[IYUU] 服务启动错误: ${err.message}`);
});
2024-06-28 21:57:14 +08:00
2024-07-01 14:08:06 +08:00
serverProcess.on("close", function (code) {
log.info("[IYUU] 服务退出:" + code);
});
}
2024-06-27 17:29:25 +08:00
}
function stopServer() {
if (serverProcess) {
2024-07-01 16:50:17 +08:00
mainWin.hideWindows()
2024-06-28 16:13:57 +08:00
log.info("Killing server process with PID:", serverProcess.pid);
try {
process.kill(-serverProcess.pid, 'SIGTERM');
log.info("后台服务已关闭...");
serverProcess = null;
} catch (err) {
log.error('Error using process.kill:', err);
2024-07-01 14:08:06 +08:00
exec(`taskkill /PID ${serverProcess.pid} /T /F`, (err, _stdout, _stderr) => {
2024-06-28 16:13:57 +08:00
if (err) {
log.error('Error using taskkill:', err);
} else {
log.info("后台服务已关闭...");
serverProcess = null;
}
});
}
2024-06-27 17:29:25 +08:00
} else {
2024-06-28 16:13:57 +08:00
log.info("No server process to kill.");
2024-06-27 17:29:25 +08:00
}
}
2024-06-27 18:27:45 +08:00
function restartServer() {
stopServer();
2024-06-28 18:37:00 +08:00
setTimeout(() => {
startServer();
2024-07-01 14:08:06 +08:00
setTimeout(() => {
mainWin.showWindows()
}, 2000)
2024-06-28 18:37:00 +08:00
}, 1000);
2024-06-27 18:27:45 +08:00
}
module.exports = { startServer, stopServer, restartServer };