think/packages/server/src/app.module.ts

122 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-06-29 16:03:02 +00:00
import { AuthEntity } from '@entities/auth.entity';
2022-05-16 09:23:59 +00:00
import { CommentEntity } from '@entities/comment.entity';
import { DocumentEntity } from '@entities/document.entity';
import { MessageEntity } from '@entities/message.entity';
2022-06-29 16:03:02 +00:00
import { OrganizationEntity } from '@entities/organization.entity';
import { StarEntity } from '@entities/star.entity';
2022-06-28 09:11:26 +00:00
import { SystemEntity } from '@entities/system.entity';
2022-05-16 09:23:59 +00:00
import { TemplateEntity } from '@entities/template.entity';
2022-02-20 11:51:55 +00:00
import { UserEntity } from '@entities/user.entity';
import { WikiEntity } from '@entities/wiki.entity';
2022-05-22 12:40:55 +00:00
import { IS_PRODUCTION } from '@helpers/env.helper';
2022-05-21 07:35:47 +00:00
import { getLogFileName, ONE_DAY } from '@helpers/log.helper';
2022-06-29 16:03:02 +00:00
import { AuthModule } from '@modules/auth.module';
2022-02-20 11:51:55 +00:00
import { CommentModule } from '@modules/comment.module';
2022-05-16 09:23:59 +00:00
import { DocumentModule } from '@modules/document.module';
import { FileModule } from '@modules/file.module';
2022-02-20 11:51:55 +00:00
import { MessageModule } from '@modules/message.module';
2022-06-29 16:03:02 +00:00
import { OrganizationModule } from '@modules/organization.module';
import { StarModule } from '@modules/star.module';
2022-06-28 09:11:26 +00:00
import { SystemModule } from '@modules/system.module';
2022-02-20 11:51:55 +00:00
import { TemplateModule } from '@modules/template.module';
2022-05-16 09:23:59 +00:00
import { UserModule } from '@modules/user.module';
2022-06-28 09:11:26 +00:00
import { VerifyModule } from '@modules/verify.module';
2022-02-20 11:51:55 +00:00
import { ViewModule } from '@modules/view.module';
2022-05-16 09:23:59 +00:00
import { WikiModule } from '@modules/wiki.module';
2022-05-21 06:39:56 +00:00
import { forwardRef, Inject, Module } from '@nestjs/common';
2022-05-16 09:23:59 +00:00
import { ConfigModule, ConfigService } from '@nestjs/config';
2022-05-21 06:39:56 +00:00
import { Cron, ScheduleModule } from '@nestjs/schedule';
2022-05-16 09:23:59 +00:00
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { getConfig } from '@think/config';
2022-05-21 06:39:56 +00:00
import * as fs from 'fs-extra';
import { LoggerModule } from 'nestjs-pino';
import * as path from 'path';
import pino from 'pino';
2022-02-20 11:51:55 +00:00
const ENTITIES = [
UserEntity,
2022-06-29 16:03:02 +00:00
AuthEntity,
OrganizationEntity,
2022-02-20 11:51:55 +00:00
WikiEntity,
DocumentEntity,
StarEntity,
2022-02-20 11:51:55 +00:00
CommentEntity,
MessageEntity,
TemplateEntity,
2022-06-28 09:11:26 +00:00
SystemEntity,
2022-02-20 11:51:55 +00:00
];
const MODULES = [
UserModule,
2022-06-29 16:03:02 +00:00
AuthModule,
OrganizationModule,
2022-02-20 11:51:55 +00:00
WikiModule,
DocumentModule,
StarModule,
2022-02-20 11:51:55 +00:00
FileModule,
CommentModule,
MessageModule,
TemplateModule,
ViewModule,
2022-06-28 09:11:26 +00:00
VerifyModule,
SystemModule,
2022-02-20 11:51:55 +00:00
];
@Module({
imports: [
ConfigModule.forRoot({
cache: true,
load: [getConfig],
isGlobal: true,
}),
2022-05-21 06:39:56 +00:00
ScheduleModule.forRoot(),
2022-05-22 12:40:55 +00:00
IS_PRODUCTION &&
2022-05-21 06:39:56 +00:00
LoggerModule.forRoot({
pinoHttp: {
stream: pino.destination({
dest: `./logs/${getLogFileName(new Date())}`,
minLength: 4096,
mkdir: true,
sync: false,
}),
},
}),
2022-02-20 11:51:55 +00:00
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => {
return {
type: 'mysql',
entities: ENTITIES,
keepConnectionAlive: true,
...config.get('db.mysql'),
} as TypeOrmModuleOptions;
},
}),
...MODULES,
2022-05-21 06:39:56 +00:00
].filter(Boolean),
2022-02-20 11:51:55 +00:00
controllers: [],
providers: [],
})
2022-05-21 06:39:56 +00:00
export class AppModule {
constructor(
@Inject(forwardRef(() => ConfigService))
private readonly configService: ConfigService
) {}
/**
* 9
*/
@Cron('0 0 9 * * *')
deleteLog() {
let retainDays = this.configService.get('server.logRetainDays');
const startDate = new Date(new Date().valueOf() - retainDays * ONE_DAY).valueOf();
do {
const filepath = path.join(__dirname, '../logs', getLogFileName(startDate, retainDays));
fs.removeSync(filepath);
retainDays -= 1;
} while (retainDays > 0);
}
}