134 lines
4.4 KiB
TypeScript
134 lines
4.4 KiB
TypeScript
import * as orm from '@midwayjs/typeorm';
|
|
import {
|
|
Configuration,
|
|
App,
|
|
IMidwayApplication,
|
|
Inject,
|
|
ILogger,
|
|
MidwayWebRouterService,
|
|
} from '@midwayjs/core';
|
|
import * as koa from '@midwayjs/koa';
|
|
// import * as crossDomain from '@midwayjs/cross-domain';
|
|
import * as validate from '@midwayjs/validate';
|
|
import * as info from '@midwayjs/info';
|
|
import * as staticFile from '@midwayjs/static-file';
|
|
import * as cron from '@midwayjs/cron';
|
|
import * as DefaultConfig from './config/config.default';
|
|
import * as LocalConfig from './config/config.local';
|
|
import * as ProdConfig from './config/config.prod';
|
|
import * as cool from '@cool-midway/core';
|
|
import * as upload from '@midwayjs/upload';
|
|
// import * as task from '@cool-midway/task';
|
|
// import * as rpc from '@cool-midway/rpc';
|
|
import { FileserverMonitorService } from './modules/center/service/fileserver-monitor';
|
|
import { CenterLogService } from './modules/center/service/logstore';
|
|
import { CenterNotifyWsService } from './modules/center/service/notify-ws';
|
|
import { CenterRbacService } from './modules/center/service/rbac';
|
|
|
|
@Configuration({
|
|
imports: [
|
|
// https://koajs.com/
|
|
koa,
|
|
// 是否开启跨域(注:顺序不能乱放!!!) http://www.midwayjs.org/docs/extensions/cross_domain
|
|
// crossDomain,
|
|
// 静态文件托管 https://midwayjs.org/docs/extensions/static_file
|
|
staticFile,
|
|
// orm https://midwayjs.org/docs/extensions/orm
|
|
orm,
|
|
// 参数验证 https://midwayjs.org/docs/extensions/validate
|
|
validate,
|
|
// 本地任务 http://www.midwayjs.org/docs/extensions/cron
|
|
cron,
|
|
// 文件上传
|
|
upload,
|
|
// cool-admin 官方组件 https://cool-js.com
|
|
cool,
|
|
// rpc 微服务 远程调用
|
|
// rpc,
|
|
// 任务与队列
|
|
// task,
|
|
{
|
|
component: info,
|
|
enabledEnvironment: ['local', 'prod'],
|
|
},
|
|
],
|
|
importConfigs: [
|
|
{
|
|
default: DefaultConfig,
|
|
local: LocalConfig,
|
|
prod: ProdConfig,
|
|
},
|
|
],
|
|
})
|
|
export class MainConfiguration {
|
|
@App()
|
|
app: IMidwayApplication;
|
|
|
|
@Inject()
|
|
webRouterService: MidwayWebRouterService;
|
|
|
|
@Inject()
|
|
logger: ILogger;
|
|
|
|
async onReady() {
|
|
// 内网文件服务器离线监控(daemon 语义:随进程启停,异常不中断业务)
|
|
const monitor = await this.app.getApplicationContext().getAsync(FileserverMonitorService);
|
|
monitor.start();
|
|
// 集中日志保留期清理(启动清理一次 + 周期清理,随进程启停)
|
|
const logStore = await this.app.getApplicationContext().getAsync(CenterLogService);
|
|
logStore.start();
|
|
// 控制台 RBAC 幂等种子(initDB/initMenu=false,兜底超管角色、业务菜单、运行期账号)
|
|
this.seedRbac();
|
|
}
|
|
|
|
/** RBAC 种子:最多重试 3 次(等数据库连接就绪),失败仅记日志不阻断启动 */
|
|
private seedRbac(attempt = 1) {
|
|
this.app
|
|
.getApplicationContext()
|
|
.getAsync(CenterRbacService)
|
|
.then(rbac => rbac.ensureSeed())
|
|
.catch(e => {
|
|
if (attempt < 3) {
|
|
setTimeout(() => this.seedRbac(attempt + 1), 3000);
|
|
return;
|
|
}
|
|
this.logger.error('[rbac] 控制台权限种子初始化失败:%s', (e as Error)?.message || e);
|
|
});
|
|
}
|
|
|
|
/** HTTP server 起来后挂载通知 WebSocket(与 8800 共用端口,执行机 notifier.js 直连) */
|
|
async onServerReady() {
|
|
try {
|
|
const framework: any = (this.app as any).getFramework?.();
|
|
const server = typeof framework?.getServer === 'function' ? framework.getServer() : null;
|
|
const wsService = await this.app.getApplicationContext().getAsync(CenterNotifyWsService);
|
|
if (!wsService.attach(server)) {
|
|
console.warn('[notify-ws] 未取到 http.Server,通知 WS 未启用(轮询兜底仍然可用)');
|
|
}
|
|
} catch (e: any) {
|
|
console.warn(`[notify-ws] 挂载异常:${e?.message || e}(轮询兜底仍然可用)`);
|
|
}
|
|
}
|
|
|
|
async onStop() {
|
|
try {
|
|
const wsService = await this.app.getApplicationContext().getAsync(CenterNotifyWsService);
|
|
wsService.stop();
|
|
} catch (e) {
|
|
/* ignore */
|
|
}
|
|
try {
|
|
const monitor = await this.app.getApplicationContext().getAsync(FileserverMonitorService);
|
|
monitor.stop();
|
|
} catch (e) {
|
|
/* ignore */
|
|
}
|
|
try {
|
|
const logStore = await this.app.getApplicationContext().getAsync(CenterLogService);
|
|
logStore.stop();
|
|
} catch (e) {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|