feat: add app serve
This commit is contained in:
		| @@ -39,7 +39,6 @@ | |||||||
|     "tslib": "^2.7.0", |     "tslib": "^2.7.0", | ||||||
|     "typescript": "^5.6.3" |     "typescript": "^5.6.3" | ||||||
|   }, |   }, | ||||||
|   "dependencies": {}, |  | ||||||
|   "resolutions": { |   "resolutions": { | ||||||
|     "picomatch": "^4" |     "picomatch": "^4" | ||||||
|   }, |   }, | ||||||
| @@ -48,5 +47,10 @@ | |||||||
|   }, |   }, | ||||||
|   "publishConfig": { |   "publishConfig": { | ||||||
|     "access": "public" |     "access": "public" | ||||||
|  |   }, | ||||||
|  |   "dependencies": { | ||||||
|  |     "@kevisual/router": "^0.0.2", | ||||||
|  |     "sequelize": "^6.37.4", | ||||||
|  |     "sqlite3": "^5.1.7" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										1178
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1178
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										65
									
								
								src/app.ts
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								src/app.ts
									
									
									
									
									
								
							| @@ -1,16 +1,57 @@ | |||||||
| import { program , Command} from 'commander'; | import { App } from '@kevisual/router'; | ||||||
| import fs from 'fs'; | import fs from 'fs'; | ||||||
|  | import { getConfig, pidFilePath, checkFileExists } from './module/get-config.ts'; | ||||||
|  | import { sequelize } from './module/sequelize.ts'; | ||||||
|  |  | ||||||
| // 将多个子命令加入主程序中 | export { sequelize }; | ||||||
| program.name('app').description('A CLI tool with envison').version('0.0.3'); |  | ||||||
|  |  | ||||||
| const ls =new Command('ls') | export const app = new App(); | ||||||
|   .description('List files in the current directory') |  | ||||||
|   .action(() => { | app | ||||||
|     console.log('List files'); |   .route({ | ||||||
|     console.log(fs.readdirSync(process.cwd())); |     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); |   import('./route.ts'); | ||||||
|  | }; | ||||||
| export const app = program; |  | ||||||
| export { program, Command }; |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import { app, Command } from '@/app.ts'; | import { program as app, Command } from '@/program.ts'; | ||||||
| import { glob } from 'glob'; | import { glob } from 'glob'; | ||||||
| import path from 'path'; | import path from 'path'; | ||||||
| import fs from 'fs'; | import fs from 'fs'; | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| import { app, program, Command } from '@/app.ts'; | import { program as app, Command } from '@/program.ts'; | ||||||
| import { getConfig, writeConfig } from '@/module/get-config.ts'; | import { getConfig, writeConfig } from '@/module/get-config.ts'; | ||||||
| import { queryLogin, queryMe, switchOrg, switchMe } from '@/route/index.ts'; | import { queryLogin, queryMe, switchOrg, switchMe } from '@/query/index.ts'; | ||||||
| import inquirer from 'inquirer'; | import inquirer from 'inquirer'; | ||||||
|  |  | ||||||
| // 导入 login 命令 | // 导入 login 命令 | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import { app, Command } from '@/app.ts'; | import { program as app, Command } from '@/program.ts'; | ||||||
| import { getConfig, writeConfig } from '@/module/index.ts'; | import { getConfig, writeConfig } from '@/module/index.ts'; | ||||||
|  |  | ||||||
| const command = new Command('logout') | const command = new Command('logout') | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import { app, Command } from '@/app.ts'; | import { program as app, Command } from '@/program.ts'; | ||||||
| import { getConfig, query, writeConfig } from '@/module/index.ts'; | import { getConfig, query, writeConfig } from '@/module/index.ts'; | ||||||
| import inquirer from 'inquirer'; | import inquirer from 'inquirer'; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| import { app, Command } from '@/app.ts'; | import { program as app, Command } from '@/program.ts'; | ||||||
| import { getConfig, writeConfig } from '@/module/index.ts'; | import { getConfig, writeConfig } from '@/module/index.ts'; | ||||||
| import {queryMe} from '../route/index.ts'; | import {queryMe} from '../query/index.ts'; | ||||||
|  |  | ||||||
|  |  | ||||||
| const command = new Command('me') | const command = new Command('me') | ||||||
|   | |||||||
							
								
								
									
										40
									
								
								src/command/serve.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/command/serve.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | |||||||
|  | import { program as app, Command } from '@/program.ts'; | ||||||
|  | import { getConfig, writeConfig, pidFilePath, checkFileExists } from '@/module/index.ts'; | ||||||
|  | import { createApp } from '@/app.ts'; | ||||||
|  | import fs from 'fs'; | ||||||
|  |  | ||||||
|  | const command = new Command('serve') | ||||||
|  |   .description('serve manager') | ||||||
|  |   .option('-e, --exit', '退出进程') | ||||||
|  |   .option('-s, --start', '启动进程') | ||||||
|  |   .action(async (options) => { | ||||||
|  |     console.log('options', options); | ||||||
|  |     if (options.exit) { | ||||||
|  |       if (checkFileExists(pidFilePath)) { | ||||||
|  |         console.log('服务已经启动, 准备退出'); | ||||||
|  |         const pid = fs.readFileSync(pidFilePath, 'utf-8'); | ||||||
|  |         // 通知进程退出 | ||||||
|  |         process.kill(Number(pid), 'SIGTERM'); | ||||||
|  |         setTimeout(() => { | ||||||
|  |           if (checkFileExists(pidFilePath)) { | ||||||
|  |             // 强制删除 pid 文件 | ||||||
|  |             fs.unlinkSync(pidFilePath); | ||||||
|  |           } | ||||||
|  |         }, 2000); | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     if (options.start) { | ||||||
|  |       const config = getConfig(); | ||||||
|  |       await createApp(); | ||||||
|  |     } | ||||||
|  |   }); | ||||||
|  |  | ||||||
|  | app.addCommand(command); | ||||||
|  |  | ||||||
|  | const start = new Command('start').description('start serve').action(async () => { | ||||||
|  |   const config = getConfig(); | ||||||
|  |   await createApp(); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | app.addCommand(start); | ||||||
| @@ -1,9 +1,9 @@ | |||||||
| import { app } from '@/app.ts'; | import { program } from '@/program.ts'; | ||||||
| import './command/login.ts'; | import './command/login.ts'; | ||||||
| import './command/logout.ts'; | import './command/logout.ts'; | ||||||
| import './command/ls-token.ts'; | import './command/ls-token.ts'; | ||||||
| import './command/me.ts'; | import './command/me.ts'; | ||||||
| import './command/deploy.ts'; | import './command/deploy.ts'; | ||||||
|  | import './command/serve.ts'; | ||||||
|  |  | ||||||
|  | program.parse(process.argv); | ||||||
| app.parse(process.argv); |  | ||||||
|   | |||||||
| @@ -5,6 +5,9 @@ import fs from 'fs'; | |||||||
| const envisionPath = path.join(os.homedir(), '.config', 'envision'); | const envisionPath = path.join(os.homedir(), '.config', 'envision'); | ||||||
| const configPath = path.join(os.homedir(), '.config', 'envision', 'config.json'); | const configPath = path.join(os.homedir(), '.config', 'envision', 'config.json'); | ||||||
|  |  | ||||||
|  | export const pidFilePath = path.join(envisionPath, 'app.pid'); | ||||||
|  | export const dbPath = path.join(envisionPath, 'db.sqlite'); | ||||||
|  |  | ||||||
| export const checkFileExists = (filePath: string) => { | export const checkFileExists = (filePath: string) => { | ||||||
|   try { |   try { | ||||||
|     fs.accessSync(filePath, fs.constants.F_OK); |     fs.accessSync(filePath, fs.constants.F_OK); | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								src/module/sequelize.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/module/sequelize.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | import { Sequelize } from 'sequelize'; | ||||||
|  | import { dbPath } from './get-config.ts'; | ||||||
|  | // connect to db | ||||||
|  | export const sequelize = new Sequelize({ | ||||||
|  |   dialect: 'sqlite', | ||||||
|  |   // storage: 'db.sqlite', | ||||||
|  |   storage: dbPath, | ||||||
|  |   // logging: false, | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | sequelize.authenticate({ logging: false }).then(() => { | ||||||
|  |   console.log('Connection sqlite has been established successfully.'); | ||||||
|  | }); | ||||||
							
								
								
									
										13
									
								
								src/program.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/program.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | import { program, Command } from 'commander'; | ||||||
|  | import fs from 'fs'; | ||||||
|  |  | ||||||
|  | // 将多个子命令加入主程序中 | ||||||
|  | program.name('app').description('A CLI tool with envison').version('0.0.3'); | ||||||
|  |  | ||||||
|  | const ls = new Command('ls').description('List files in the current directory').action(() => { | ||||||
|  |   console.log('List files'); | ||||||
|  |   console.log(fs.readdirSync(process.cwd())); | ||||||
|  | }); | ||||||
|  | program.addCommand(ls); | ||||||
|  |  | ||||||
|  | export { program, Command }; | ||||||
							
								
								
									
										1
									
								
								src/route.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/route.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | import './route/user/list.ts'; | ||||||
							
								
								
									
										11
									
								
								src/route/user/list.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/route/user/list.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | import { app } from '@/app.ts'; | ||||||
|  |  | ||||||
|  | app | ||||||
|  |   .route({ | ||||||
|  |     path: 'user', | ||||||
|  |     key: 'list', | ||||||
|  |   }) | ||||||
|  |   .define(async (ctx) => { | ||||||
|  |     ctx.body = 'user list'; | ||||||
|  |   }) | ||||||
|  |   .addTo(app); | ||||||
		Reference in New Issue
	
	Block a user