envision-cli/src/app.ts

55 lines
1.7 KiB
TypeScript

import { App } from '@kevisual/router';
import fs from 'fs';
import { getConfig, getPidList, pidFilePath, checkFileExists } from './module/get-config.ts';
import { sequelize } from './module/sequelize.ts';
export { sequelize };
export const app = new App({
serverOptions: {
cors: {
origin: '*', // 允许所有来源
},
httpType: 'https',
},
});
// 处理进程退出或中断信号,确保删除 pid 文件
const cleanUp = () => {
const pidList = getPidList();
const findPid = pidList.find((item) => Number(item.pid) === process.pid);
// if (checkFileExists(pidFilePath)) {
// const pid = fs.readFileSync(pidFilePath, 'utf-8');
// if (Number(pid) === process.pid) {
// fs.unlinkSync(pidFilePath);
// console.log('server id', process.pid, 'is exit');
// }
// }
if (findPid) {
console.log('server id', process.pid, 'is exit');
try {
fs.unlinkSync(findPid.file);
} catch (e) {
console.log('unlinkSync error', findPid);
}
}
process.exit(0); // 退出进程
};
// 当进程收到以下信号时,删除 pid 文件
process.on('SIGINT', cleanUp); // 例如 Ctrl+C
process.on('SIGTERM', cleanUp); // 终止信号
process.on('exit', cleanUp); // 进程退出
export const createApp = async () => {
if (checkFileExists(pidFilePath)) {
const pid = fs.readFileSync(pidFilePath, 'utf-8');
console.log('服务已经启动,请勿重复启动。', 'server id', pid);
return;
}
fs.writeFileSync(pidFilePath, process.pid.toString());
app.listen(21015, () => {
console.log('Server is running on port 21015', 'https://localhost:21015/api/router', 'server id', process.pid);
});
// import('./route.ts');
};