IYUUPlus-Windows/server/server.js

112 lines
3.2 KiB
JavaScript
Raw 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-06-27 17:29:25 +08:00
2024-06-28 12:21:47 +08:00
function getCmdPath(resourcePath) {
2024-06-27 17:29:25 +08:00
const arch = os.arch();
if (arch === 'x64') {
2024-06-28 12:21:47 +08:00
return path.join(resourcePath, 'run', 'php-8.3.8-x64', 'php.exe');
2024-06-27 17:29:25 +08:00
} else if (arch === 'ia32') {
2024-06-28 12:21:47 +08:00
return path.join(resourcePath, 'run', 'php-8.3.8-x86', 'php.exe');
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);
}
}
let serverProcess = null;
function startServer() {
2024-06-28 12:21:47 +08:00
let resourcePath = process.resourcesPath;
if (resourcePath.includes('node_modules')) {
resourcePath = process.cwd();
}
const cmdPath = getCmdPath(resourcePath);
2024-06-28 18:37:00 +08:00
const batPath = path.join(resourcePath, 'run.bat');
2024-06-28 12:21:47 +08:00
const workingDirectory = path.resolve(resourcePath);
2024-06-27 17:29:25 +08:00
2024-06-27 18:27:45 +08:00
const env = { ...process.env };
const phpDir = path.dirname(cmdPath);
2024-06-28 18:37:00 +08:00
env.Path = `${phpDir};${env.Path}`;
2024-06-27 18:27:45 +08:00
2024-06-28 18:37:00 +08:00
// serverProcess = exec(`C:\\Windows\\System32\\cmd.exe /c ${batPath}`, {
// cwd: workingDirectory,
// env: env,
// windowsHide: true
// }, (error, stdout, stderr) => {
// if (error) {
// log.error(`[IYUU] 服务启动错误: ${error.message}`);
// return;
// }
// if (stderr) {
// log.error(`[IYUU] 服务启动 stderr: ${stderr}`);
// return;
// }
// log.info(`[IYUU] 服务启动成功: ${stdout}`);
// });
serverProcess = execFile('cmd.exe', ['/c', batPath], {
2024-06-27 17:29:25 +08:00
cwd: workingDirectory,
2024-06-28 18:37:00 +08:00
stdio: 'ignore',
2024-06-28 12:21:47 +08:00
env: env,
2024-06-28 18:37:00 +08:00
windowsHide: true
2024-06-27 17:29:25 +08:00
});
2024-06-28 18:37:00 +08:00
// serverProcess = spawn('cmd.exe', ['/c', batPath], {
// cwd: workingDirectory,
// stdio: 'ignore',
// env: env,
// detached: true,
// windowsHide: true
// });
serverProcess.unref();
if (serverProcess.stdout) {
serverProcess.stdout.setEncoding('utf8');
serverProcess.stdout.on("data", function (data) {
log.info("[IYUU] 服务启动成功");
});
}
2024-06-27 17:29:25 +08:00
serverProcess.on("close", function (code) {
2024-06-28 16:13:57 +08:00
log.info("[IYUU] 服务退出:" + code);
2024-06-27 17:29:25 +08:00
});
}
function stopServer() {
if (serverProcess) {
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);
log.info('Falling back to taskkill');
exec(`taskkill /PID ${serverProcess.pid} /T /F`, (err, stdout, stderr) => {
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();
}, 1000);
2024-06-27 18:27:45 +08:00
}
module.exports = { startServer, stopServer, restartServer };