xiaoju-survey/server/src/app.module.ts

128 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-01-30 14:19:45 +00:00
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { ResponseSecurityPlugin } from './securityPlugin/responseSecurityPlugin';
import { SurveyUtilPlugin } from './securityPlugin/surveyUtilPlugin';
2024-01-30 14:19:45 +00:00
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ServeStaticModule } from '@nestjs/serve-static';
import { SurveyModule } from './modules/survey/survey.module';
import { SurveyResponseModule } from './modules/surveyResponse/surveyResponse.module';
import { AuthModule } from './modules/auth/auth.module';
2024-04-25 05:44:36 +00:00
import { MessageModule } from './modules/message/message.module';
import { FileModule } from './modules/file/file.module';
2024-01-30 14:19:45 +00:00
import { join } from 'path';
import { APP_FILTER } from '@nestjs/core';
import { HttpExceptionsFilter } from './exceptions/httpExceptions.filter';
import { Captcha } from './models/captcha.entity';
import { User } from './models/user.entity';
import { SurveyMeta } from './models/surveyMeta.entity';
import { SurveyConf } from './models/surveyConf.entity';
import { SurveyHistory } from './models/surveyHistory.entity';
import { ResponseSchema } from './models/responseSchema.entity';
import { Counter } from './models/counter.entity';
import { SurveyResponse } from './models/surveyResponse.entity';
import { ClientEncrypt } from './models/clientEncrypt.entity';
import { Word } from './models/word.entity';
2024-03-28 13:37:59 +00:00
import { MessagePushingTask } from './models/messagePushingTask.entity';
import { MessagePushingLog } from './models/messagePushingLog.entity';
2024-01-30 14:19:45 +00:00
import { LoggerProvider } from './logger/logger.provider';
import { PluginManagerProvider } from './securityPlugin/pluginManager.provider';
2024-01-30 14:19:45 +00:00
import { LogRequestMiddleware } from './middlewares/logRequest.middleware';
import { XiaojuSurveyPluginManager } from './securityPlugin/pluginManager';
2024-01-30 14:19:45 +00:00
import { Logger } from './logger';
@Module({
imports: [
ConfigModule.forRoot({}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const url = await configService.get<string>('XIAOJU_SURVEY_MONGO_URL');
const authSource =
(await configService.get<string>(
'XIAOJU_SURVEY_MONGO_AUTH_SOURCE',
2024-03-14 13:50:57 +00:00
)) || 'admin';
2024-01-30 14:19:45 +00:00
const database = await configService.get<string>(
'XIAOJU_SURVEY_MONGO_DB_NAME',
);
return {
type: 'mongodb',
connectTimeoutMS: 10000,
socketTimeoutMS: 10000,
url,
authSource,
useNewUrlParser: true,
database,
entities: [
Captcha,
User,
SurveyMeta,
SurveyConf,
SurveyHistory,
SurveyResponse,
Counter,
ResponseSchema,
ClientEncrypt,
Word,
2024-03-28 13:37:59 +00:00
MessagePushingTask,
MessagePushingLog,
2024-01-30 14:19:45 +00:00
],
};
},
}),
AuthModule,
SurveyModule,
SurveyResponseModule,
2024-04-25 05:44:36 +00:00
ServeStaticModule.forRootAsync({
useFactory: async () => {
return [
{
rootPath: join(__dirname, '..', 'public'),
},
];
},
2024-01-30 14:19:45 +00:00
}),
2024-04-25 05:44:36 +00:00
MessageModule,
FileModule,
2024-01-30 14:19:45 +00:00
],
controllers: [AppController],
providers: [
{
provide: APP_FILTER,
useClass: HttpExceptionsFilter,
},
LoggerProvider,
PluginManagerProvider,
],
})
export class AppModule {
constructor(
private readonly configService: ConfigService,
private readonly pluginManager: XiaojuSurveyPluginManager,
) {}
configure(consumer: MiddlewareConsumer) {
consumer.apply(LogRequestMiddleware).forRoutes('*');
}
onModuleInit() {
this.pluginManager.registerPlugin(
new ResponseSecurityPlugin(
this.configService.get<string>(
'XIAOJU_SURVEY_RESPONSE_AES_ENCRYPT_SECRET_KEY',
),
),
new SurveyUtilPlugin(),
);
2024-03-14 13:50:57 +00:00
Logger.init({
2024-01-30 14:19:45 +00:00
filename: this.configService.get<string>('XIAOJU_SURVEY_LOGGER_FILENAME'),
});
}
}