1- import fastifyStatic from '@fastify/static' ;
21import * as fastlogger from './controllers/fastlogger.js'
32import path from 'path' ;
43import os from 'os' ;
@@ -19,61 +18,25 @@ const {fastify} = fastlogger;
1918const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
2019const PORT = 5757 ;
2120const MAX_TEXT_SIZE = 0.1 * 1024 * 1024 ; // 设置最大文本大小为 0.1 MB
21+ // 定义options的目录
22+ const docsDir = path . join ( __dirname , 'docs' ) ;
23+ const jxDir = path . join ( __dirname , 'jx' ) ;
24+ const publicDir = path . join ( __dirname , 'public' ) ;
25+ const appsDir = path . join ( __dirname , 'apps' ) ;
2226const jsonDir = path . join ( __dirname , 'json' ) ;
2327const jsDir = path . join ( __dirname , 'spider/js' ) ;
2428const dr2Dir = path . join ( __dirname , 'spider/js_dr2' ) ;
2529const pyDir = path . join ( __dirname , 'spider/py' ) ;
2630const catDir = path . join ( __dirname , 'spider/catvod' ) ;
31+ const catLibDir = path . join ( __dirname , 'spider/catLib' ) ;
2732const xbpqDir = path . join ( __dirname , 'spider/xbpq' ) ;
28-
29- // 静态资源
30- fastify . register ( fastifyStatic , {
31- root : path . join ( __dirname , 'public' ) ,
32- prefix : '/public/' ,
33- } ) ;
34-
35- fastify . register ( fastifyStatic , {
36- root : path . join ( __dirname , 'apps' ) ,
37- prefix : '/apps/' , // 新的访问路径前缀
38- decorateReply : false , // 禁用 sendFile
39- } ) ;
40-
41- fastify . register ( fastifyStatic , {
42- root : jsonDir ,
43- prefix : '/json/' , // 新的访问路径前缀
44- decorateReply : false , // 禁用 sendFile
45- } ) ;
46-
47- fastify . register ( fastifyStatic , {
48- root : dr2Dir ,
49- prefix : '/js/' , // 新的访问路径前缀
50- decorateReply : false , // 禁用 sendFile
51- // setHeaders: (res, path) => {
52- // res.setHeader('Cache-Control', 'no-store'); // 禁用缓存确保每次获取最新
53- // }
54- } ) ;
55-
56- fastify . register ( fastifyStatic , {
57- root : pyDir ,
58- prefix : '/py/' , // 新的访问路径前缀
59- decorateReply : false , // 禁用 sendFile
60- setHeaders : ( res , path ) => {
61- // 自定义 .py 文件的 Content-Type
62- if ( path . endsWith ( '.py' ) ) {
63- res . setHeader ( 'Content-Type' , 'text/plain; charset=utf-8' )
64- }
65- }
66- } ) ;
67-
68- fastify . register ( fastifyStatic , {
69- root : catDir ,
70- prefix : '/cat/' , // 新的访问路径前缀
71- decorateReply : false , // 禁用 sendFile
72- } ) ;
33+ const viewsDir = path . join ( __dirname , 'views' ) ;
34+ const configDir = path . join ( __dirname , 'config' ) ;
7335
7436// 注册插件以支持 application/x-www-form-urlencoded
7537fastify . register ( formBody ) ;
7638
39+ // 添加钩子事件
7740fastify . addHook ( 'onReady' , async ( ) => {
7841 try {
7942 await daemon . startDaemon ( ) ;
@@ -124,32 +87,74 @@ fastify.addHook('onRequest', async (req, reply) => {
12487 // 如果需要,可以在这里对 req.query 进行进一步处理
12588} ) ;
12689
90+ process . on ( 'unhandledRejection' , ( err ) => {
91+ fastify . log . error ( `未处理的Promise拒绝:${ err . message } ` ) ;
92+ console . log ( `发生了致命的错误,已阻止进程崩溃。${ err . stack } ` ) ;
93+ // 根据情况决定是否退出进程
94+ // 清理后退出进程(避免程序处于未知状态)
95+ // process.exit(1);
96+ } ) ;
97+
98+ // 统一退出处理函数
99+ const handleExit = async ( signal ) => {
100+ try {
101+ console . log ( `\nReceived ${ signal } , closing server...` ) ;
102+ // Fastify 提供的关闭方法,内部会触发 onClose 钩子
103+ await fastify . close ( ) ;
104+ console . log ( 'Fastify closed successfully' ) ;
105+ process . exit ( 0 ) ;
106+ } catch ( err ) {
107+ console . error ( 'Error during shutdown:' , err ) ;
108+ process . exit ( 1 ) ;
109+ }
110+ } ;
111+
112+ // 捕获常见退出信号(Linux 上 pm2 stop 会发 SIGINT 或 SIGTERM)
113+ [ 'SIGINT' , 'SIGTERM' ] . forEach ( ( sig ) => {
114+ process . on ( sig , ( ) => handleExit ( sig ) ) ;
115+ } ) ;
116+
117+ // Windows 上的兼容处理:捕获 Ctrl+C
118+ if ( process . platform === 'win32' ) {
119+ const rl = ( await import ( 'readline' ) ) . createInterface ( {
120+ input : process . stdin ,
121+ output : process . stdout ,
122+ } ) ;
123+
124+ rl . on ( 'SIGINT' , ( ) => {
125+ handleExit ( 'SIGINT' ) ;
126+ } ) ;
127+ }
128+
129+ // 捕获 Node.js 主动退出(比如 pm2 stop 也会触发 exit)
130+ process . on ( 'exit' , async ( code ) => {
131+ console . log ( `Process exiting with code: ${ code } ` ) ;
132+ // 这里不能直接用 await fastify.close()(Node 在 exit 里不等异步)
133+ // 但 Fastify 的 SIGINT/SIGTERM 会提前触发,所以这里只记录日志
134+ } ) ;
135+
127136registerRoutes ( fastify , {
128137 rootDir : __dirname ,
129- docsDir : path . join ( __dirname , 'docs' ) ,
130- jxDir : path . join ( __dirname , 'jx' ) ,
131- jsonDir : jsonDir ,
132- jsDir : jsDir ,
133- dr2Dir : dr2Dir ,
134- pyDir : pyDir ,
135- catDir : catDir ,
136- xbpqDir : xbpqDir ,
137- viewsDir : path . join ( __dirname , 'views' ) ,
138- configDir : path . join ( __dirname , 'config' ) ,
138+ docsDir,
139+ jxDir,
140+ publicDir,
141+ appsDir,
142+ jsonDir,
143+ jsDir,
144+ dr2Dir,
145+ pyDir,
146+ catDir,
147+ catLibDir,
148+ xbpqDir,
139149 PORT ,
140150 MAX_TEXT_SIZE ,
151+ viewsDir,
152+ configDir,
141153 indexFilePath : path . join ( __dirname , 'index.json' ) ,
142154 customFilePath : path . join ( __dirname , 'custom.json' ) ,
143155 subFilePath : path . join ( __dirname , 'public/sub/sub.json' ) ,
144156} ) ;
145157
146- process . on ( 'unhandledRejection' , ( err ) => {
147- fastify . log . error ( `未处理的Promise拒绝:${ err . message } ` ) ;
148- console . log ( `发生了致命的错误,已阻止进程崩溃。${ err . stack } ` ) ;
149- // 根据情况决定是否退出进程
150- // 清理后退出进程(避免程序处于未知状态)
151- // process.exit(1);
152- } ) ;
153158
154159// 启动服务
155160const start = async ( ) => {
0 commit comments