修复启动逻辑

This commit is contained in:
whyour 2025-05-17 17:25:50 +08:00
parent ec3d61a713
commit 472a3088df
4 changed files with 34 additions and 18 deletions

View File

@ -7,26 +7,23 @@ import { Container } from 'typedi';
import config from './config'; import config from './config';
import Logger from './loaders/logger'; import Logger from './loaders/logger';
import { monitoringMiddleware } from './middlewares/monitoring'; import { monitoringMiddleware } from './middlewares/monitoring';
import { GrpcServerService } from './services/grpc'; import { type GrpcServerService } from './services/grpc';
import { HttpServerService } from './services/http'; import { type HttpServerService } from './services/http';
import { metricsService } from './services/metrics';
class Application { class Application {
private app: express.Application; private app: express.Application;
private server: any; private httpServerService?: HttpServerService;
private httpServerService: HttpServerService; private grpcServerService?: GrpcServerService;
private grpcServerService: GrpcServerService;
private isShuttingDown = false; private isShuttingDown = false;
constructor() { constructor() {
this.app = express(); this.app = express();
this.httpServerService = Container.get(HttpServerService);
this.grpcServerService = Container.get(GrpcServerService);
} }
async start() { async start() {
try { try {
await this.initializeDatabase(); await this.initializeDatabase();
await this.initServer();
this.setupMiddlewares(); this.setupMiddlewares();
await this.initializeServices(); await this.initializeServices();
this.setupGracefulShutdown(); this.setupGracefulShutdown();
@ -38,6 +35,13 @@ class Application {
} }
} }
async initServer() {
const { HttpServerService } = await import('./services/http');
const { GrpcServerService } = await import('./services/grpc');
this.httpServerService = Container.get(HttpServerService);
this.grpcServerService = Container.get(GrpcServerService);
}
private async initializeDatabase() { private async initializeDatabase() {
await require('./loaders/db').default(); await require('./loaders/db').default();
} }
@ -50,16 +54,16 @@ class Application {
} }
private async initializeServices() { private async initializeServices() {
await this.grpcServerService.initialize(); await this.grpcServerService?.initialize();
await require('./loaders/app').default({ app: this.app }); await require('./loaders/app').default({ app: this.app });
this.server = await this.httpServerService.initialize( const server = await this.httpServerService?.initialize(
this.app, this.app,
config.port, config.port,
); );
await require('./loaders/server').default({ server: this.server }); await require('./loaders/server').default({ server });
} }
private setupGracefulShutdown() { private setupGracefulShutdown() {
@ -70,8 +74,8 @@ class Application {
Logger.info('Shutting down services...'); Logger.info('Shutting down services...');
try { try {
await Promise.all([ await Promise.all([
this.grpcServerService.shutdown(), this.grpcServerService?.shutdown(),
this.httpServerService.shutdown(), this.httpServerService?.shutdown(),
]); ]);
process.exit(0); process.exit(0);
} catch (error) { } catch (error) {

View File

@ -13,7 +13,7 @@ import { AuthDataType, SystemModel } from '../data/system';
import SystemService from '../services/system'; import SystemService from '../services/system';
import UserService from '../services/user'; import UserService from '../services/user';
import { writeFile, readFile } from 'fs/promises'; import { writeFile, readFile } from 'fs/promises';
import { createRandomString, safeJSONParse } from '../config/util'; import { createRandomString, fileExist, safeJSONParse } from '../config/util';
import OpenService from '../services/open'; import OpenService from '../services/open';
import { shareStore } from '../shared/store'; import { shareStore } from '../shared/store';
import Logger from './logger'; import Logger from './logger';
@ -56,8 +56,11 @@ export default async () => {
password: 'admin', password: 'admin',
}; };
try { try {
const content = await readFile(config.authConfigFile, 'utf8'); const authFileExist = await fileExist(config.authConfigFile);
authInfo = safeJSONParse(content); if (authFileExist) {
const content = await readFile(config.authConfigFile, 'utf8');
authInfo = safeJSONParse(content);
}
} catch (error) { } catch (error) {
Logger.warn('Failed to read auth config file, using default credentials'); Logger.warn('Failed to read auth config file, using default credentials');
} }

View File

@ -110,7 +110,10 @@ export default function () {
} }
}) })
.catch((error) => { .catch((error) => {
history.push('/error'); const responseStatus = error.response.status;
if (responseStatus !== 401) {
history.push('/error');
}
}) })
.finally(() => setInitLoading(false)); .finally(() => setInitLoading(false));
}; };

View File

@ -36,6 +36,12 @@ const Error = () => {
getHealthStatus(false); getHealthStatus(false);
}, 3000); }, 3000);
}) })
.catch((error) => {
const responseStatus = error.response.status;
if (responseStatus === 401) {
history.push('/login');
}
})
.finally(() => needLoading && setLoading(false)); .finally(() => needLoading && setLoading(false));
}; };
@ -74,7 +80,7 @@ const Error = () => {
<div>{intl.get('2. 容器内执行 ql check、ql update')}</div> <div>{intl.get('2. 容器内执行 ql check、ql update')}</div>
<div> <div>
{intl.get( {intl.get(
'3. 如果无法解决,容器内执行 pm2 logs拷贝执行结果' '3. 如果无法解决,容器内执行 pm2 logs拷贝执行结果',
)} )}
<Typography.Link href="https://github.com/whyour/qinglong/issues/new?assignees=&labels=&template=bug_report.yml"> <Typography.Link href="https://github.com/whyour/qinglong/issues/new?assignees=&labels=&template=bug_report.yml">
{intl.get('提交 issue')} {intl.get('提交 issue')}