Files
certd/packages/ui/certd-server/src/modules/auto/auto-init-site.ts
T
xiaojunnuo 4755216505 perf: 重构自动加载模块并优化EAB授权处理
refactor(ui): 将分散的auto-*模块整合为统一命名的auto-register模块
perf(plugin-cert): 增强EAB授权功能,支持账号私钥刷新和类型选择
test: 添加EAB授权服务和ACME账号配置的单元测试
docs: 更新AGENTS.md补充ACME/EAB使用注意事项
chore: 统一各package.json中的测试脚本配置
2026-05-10 16:57:12 +08:00

90 lines
2.8 KiB
TypeScript

import { logger } from '@certd/basic';
import { PlusService, SysInstallInfo, SysPrivateSettings, SysSettingsService } from '@certd/lib-server';
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import crypto from 'crypto';
import { nanoid } from 'nanoid';
import { UserService } from '../sys/authority/service/user-service.js';
import { SafeService } from "../sys/settings/safe-service.js";
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class AutoInitSite {
@Inject()
userService: UserService;
@Config('typeorm.dataSource.default.type')
dbType: string;
@Inject()
sysSettingsService: SysSettingsService;
@Inject()
plusService: PlusService;
@Inject()
safeService: SafeService;
async init() {
logger.info('初始化站点开始');
await this.startOptimizeDb();
//安装信息
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
if (!installInfo.siteId) {
installInfo.siteId = nanoid();
await this.sysSettingsService.saveSetting(installInfo);
}
//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);
}
await this.sysSettingsService.backupSecret();
//加载一次密钥
await this.sysSettingsService.getSecret();
// 授权许可
try {
await this.plusService.verify();
} catch (e) {
logger.error('授权许可验证失败', e);
}
//加载设置
await this.sysSettingsService.reloadSettings();
//加载站点隐藏配置
await this.safeService.reloadHiddenStatus(true)
logger.info('初始化站点完成');
}
async startOptimizeDb() {
//优化数据库
//检查当前数据库类型为sqlite
if (this.dbType === 'better-sqlite3') {
const res = await this.userService.repository.query('PRAGMA auto_vacuum;');
if (!(res && res.length > 0 && res[0].auto_vacuum > 0)) {
//未开启自动优化
await this.userService.repository.query('PRAGMA auto_vacuum = INCREMENTAL;');
logger.info('sqlite数据库自动优化已开启');
}
const optimizeDb = async () => {
logger.info('sqlite数据库空间优化开始');
await this.userService.repository.query('VACUUM');
logger.info('sqlite数据库空间优化完成');
};
await optimizeDb();
setInterval(optimizeDb, 1000 * 60 * 60 * 24);
}
}
}