2024-08-29 09:57:27 +08:00
|
|
|
import { Autoload, Config, Init, Inject, Scope, ScopeEnum } from '@midwayjs/core';
|
2024-07-15 00:30:33 +08:00
|
|
|
import { logger } from '../../utils/logger.js';
|
|
|
|
|
import { UserService } from '../authority/service/user-service.js';
|
|
|
|
|
import { SysSettingsService } from '../system/service/sys-settings-service.js';
|
|
|
|
|
import { nanoid } from 'nanoid';
|
2024-08-27 13:46:19 +08:00
|
|
|
import { SysInstallInfo, SysLicenseInfo, SysPrivateSettings } from '../system/service/models.js';
|
2024-08-02 22:58:29 +08:00
|
|
|
import { verify } from '@certd/pipeline';
|
2024-08-27 13:46:19 +08:00
|
|
|
import crypto from 'crypto';
|
2024-07-15 00:30:33 +08:00
|
|
|
export type InstallInfo = {
|
|
|
|
|
installTime: number;
|
|
|
|
|
instanceId?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Autoload()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
|
|
|
|
export class AutoInitSite {
|
|
|
|
|
@Inject()
|
|
|
|
|
userService: UserService;
|
|
|
|
|
|
2024-08-29 09:57:27 +08:00
|
|
|
@Config('typeorm.dataSource.default.type')
|
|
|
|
|
dbType: string;
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
|
|
|
|
|
|
|
|
|
@Init()
|
|
|
|
|
async init() {
|
|
|
|
|
logger.info('初始化站点开始');
|
2024-08-29 09:57:27 +08:00
|
|
|
this.startOptimizeDb();
|
2024-08-27 13:46:19 +08:00
|
|
|
//安装信息
|
2024-07-15 00:30:33 +08:00
|
|
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
|
|
|
|
if (!installInfo.siteId) {
|
|
|
|
|
installInfo.siteId = nanoid();
|
|
|
|
|
await this.sysSettingsService.saveSetting(installInfo);
|
|
|
|
|
}
|
|
|
|
|
if (!installInfo.siteId) {
|
|
|
|
|
installInfo.siteId = nanoid();
|
|
|
|
|
await this.sysSettingsService.saveSetting(installInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 13:46:19 +08:00
|
|
|
//private信息
|
|
|
|
|
const privateInfo = await this.sysSettingsService.getSetting<SysPrivateSettings>(SysPrivateSettings);
|
|
|
|
|
if (!privateInfo.jwtKey) {
|
|
|
|
|
privateInfo.jwtKey = nanoid();
|
|
|
|
|
await this.sysSettingsService.saveSetting(privateInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!privateInfo.encryptSecret) {
|
|
|
|
|
const secretKey = crypto.randomBytes(32);
|
|
|
|
|
privateInfo.encryptSecret = secretKey.toString('base64');
|
|
|
|
|
await this.sysSettingsService.saveSetting(privateInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 22:58:29 +08:00
|
|
|
// 授权许可
|
|
|
|
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
2024-08-03 23:32:50 +08:00
|
|
|
const req = {
|
|
|
|
|
subjectId: installInfo.siteId,
|
|
|
|
|
license: licenseInfo.license,
|
|
|
|
|
};
|
|
|
|
|
await verify(req);
|
2024-08-02 22:58:29 +08:00
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
logger.info('初始化站点完成');
|
|
|
|
|
}
|
2024-08-29 09:57:27 +08:00
|
|
|
|
|
|
|
|
startOptimizeDb() {
|
|
|
|
|
//优化数据库
|
|
|
|
|
//检查当前数据库类型为sqlite
|
|
|
|
|
if (this.dbType === 'better-sqlite3') {
|
|
|
|
|
const optimizeDb = () => {
|
|
|
|
|
this.userService.repository.query('VACUUM');
|
|
|
|
|
logger.info('sqlite数据库空间优化完成');
|
|
|
|
|
};
|
|
|
|
|
optimizeDb();
|
|
|
|
|
setInterval(optimizeDb, 1000 * 60 * 60 * 24);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-15 00:30:33 +08:00
|
|
|
}
|