@@ -18,61 +18,75 @@ import { setupGlobalErrorHandling } from './utils/error';
1818
1919const requestSizeLimit = `${ Number ( process . env . MAX_API_SIZE || 10 ) } mb` ;
2020
21- const app = express ( ) . use (
22- express . json ( { limit : requestSizeLimit } ) ,
23- express . urlencoded ( { extended : true , limit : requestSizeLimit } ) ,
24- express . static ( isProd ? 'public' : join ( basePath , 'dist' , 'public' ) , {
25- maxAge : isProd ? '1d' : '0' ,
26- etag : true ,
27- lastModified : true
28- } )
29- ) ;
21+ async function main ( reboot : boolean = false ) {
22+ const app = express ( ) . use (
23+ express . json ( { limit : requestSizeLimit } ) ,
24+ express . urlencoded ( { extended : true , limit : requestSizeLimit } ) ,
25+ express . static ( isProd ? 'public' : join ( basePath , 'dist' , 'public' ) , {
26+ maxAge : isProd ? '1d' : '0' ,
27+ etag : true ,
28+ lastModified : true
29+ } )
30+ ) ;
3031
31- connectSignoz ( ) ;
32+ connectSignoz ( ) ;
3233
33- // System
34- initOpenAPI ( app ) ;
35- initRouter ( app ) ;
36- setupProxy ( ) ;
34+ // System
35+ initOpenAPI ( app ) ;
36+ initRouter ( app ) ;
37+ setupProxy ( ) ;
3738
38- // DB
39- try {
40- await connectMongo ( connectionMongo , MONGO_URL ) ;
41- } catch ( error ) {
42- addLog . error ( 'Failed to initialize services:' , error ) ;
43- process . exit ( 1 ) ;
44- }
39+ // DB
40+ try {
41+ await connectMongo ( connectionMongo , MONGO_URL ) ;
42+ } catch ( error ) {
43+ addLog . error ( 'Failed to initialize services:' , error ) ;
44+ process . exit ( 1 ) ;
45+ }
4546
46- await initializeS3 ( ) ;
47+ await initializeS3 ( ) ;
4748
48- // Modules
49- await refreshDir ( tempDir ) ; // upload pkg files, unpkg, temp dir
50- await ensureDir ( tempToolsDir ) ; // ensure the unpkged tools temp dir
49+ // Modules
50+ await refreshDir ( tempDir ) ; // upload pkg files, unpkg, temp dir
51+ await ensureDir ( tempToolsDir ) ; // ensure the unpkged tools temp dir
5152
52- await Promise . all ( [
53- getCachedData ( SystemCacheKeyEnum . systemTool ) , // init system tool
54- initModels ( ) ,
55- initWorkflowTemplates ( )
56- ] ) ;
53+ await Promise . all ( [
54+ getCachedData ( SystemCacheKeyEnum . systemTool ) , // init system tool
55+ initModels ( reboot ) ,
56+ initWorkflowTemplates ( )
57+ ] ) ;
5758
58- const PORT = parseInt ( process . env . PORT || '3000' ) ;
59- const server = app . listen ( PORT , ( error ?: Error ) => {
60- if ( error ) {
61- console . error ( error ) ;
62- process . exit ( 1 ) ;
63- }
64- addLog . info ( `FastGPT Plugin Service is listening at http://localhost:${ PORT } ` ) ;
65- } ) ;
59+ const PORT = parseInt ( process . env . PORT || '3000' ) ;
60+ const server = app . listen ( PORT , ( error ?: Error ) => {
61+ if ( error ) {
62+ console . error ( error ) ;
63+ process . exit ( 1 ) ;
64+ }
65+ addLog . info ( `FastGPT Plugin Service is listening at http://localhost:${ PORT } ` ) ;
66+ } ) ;
6667
67- [ 'SIGTERM' , 'SIGINT' ] . forEach ( ( signal ) =>
68- process . on ( signal , ( ) => {
69- addLog . debug ( `${ signal } signal received: closing HTTP server` ) ;
70- server . close ( ( ) => {
71- addLog . info ( 'HTTP server closed' ) ;
72- process . exit ( 0 ) ;
73- } ) ;
74- } )
75- ) ;
68+ [ 'SIGTERM' , 'SIGINT' ] . forEach ( ( signal ) =>
69+ process . on ( signal , ( ) => {
70+ addLog . debug ( `${ signal } signal received: closing HTTP server` ) ;
71+ server . close ( ( ) => {
72+ addLog . info ( 'HTTP server closed' ) ;
73+ process . exit ( 0 ) ;
74+ } ) ;
75+ } )
76+ ) ;
7677
77- // 全局错误处理设置
78- setupGlobalErrorHandling ( app ) ;
78+ // 全局错误处理设置
79+ setupGlobalErrorHandling ( app ) ;
80+ }
81+
82+ if ( import . meta. main ) {
83+ // get the arguments from the command line
84+ const args = process . argv . slice ( 2 ) ;
85+ const reboot = args . includes ( '--reboot' ) ;
86+ global . isReboot = reboot ;
87+ await main ( reboot ) ;
88+ }
89+
90+ declare global {
91+ var isReboot : boolean ;
92+ }
0 commit comments