feat: add app serve

This commit is contained in:
2024-10-17 01:18:03 +08:00
parent edb0d56094
commit 5c3f454d8c
17 changed files with 1314 additions and 36 deletions

View File

@@ -1,16 +1,57 @@
import { program , Command} from 'commander';
import { App } from '@kevisual/router';
import fs from 'fs';
import { getConfig, pidFilePath, checkFileExists } from './module/get-config.ts';
import { sequelize } from './module/sequelize.ts';
// 将多个子命令加入主程序中
program.name('app').description('A CLI tool with envison').version('0.0.3');
export { sequelize };
const ls =new Command('ls')
.description('List files in the current directory')
.action(() => {
console.log('List files');
console.log(fs.readdirSync(process.cwd()));
export const app = new App();
app
.route({
path: 'ping',
})
.define(async (ctx) => {
ctx.body = 'pong';
})
.addTo(app);
app
.route({
path: 'demo',
key: '01',
})
.define(async (ctx) => {
ctx.body = 'Hello, World!';
})
.addTo(app);
// 处理进程退出或中断信号,确保删除 pid 文件
const cleanUp = () => {
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');
}
}
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', 'http://localhost:21015/api/router', 'server id', process.pid);
});
program.addCommand(ls);
export const app = program;
export { program, Command };
import('./route.ts');
};