| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-15 14:17:16 |
| 4 | * @LastEditTime: 2025-04-27 17:37:13 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import * as Joi from '@hapi/joi'; |
| 9 | import { |
| 10 | MiddlewareConsumer, |
| 11 | Module, |
| 12 | NestModule, |
| 13 | RequestMethod, |
| 14 | } from '@nestjs/common'; |
| 15 | import { ConfigModule, ConfigService } from '@nestjs/config'; |
| 16 | import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core'; |
| 17 | import { EventEmitterModule } from '@nestjs/event-emitter'; |
| 18 | import { ScheduleModule } from '@nestjs/schedule'; |
| 19 | import bullMqConfig from '../config/bullMq.config'; |
| 20 | import serverConfig from '../config/server.config'; |
| 21 | import googleConfig from '../config/google.config'; |
| 22 | import { AppController } from './app.controller'; |
| 23 | import { AppService } from './app.service'; |
| 24 | import { AuthModule } from './auth/auth.module'; |
| 25 | import { DbMongoModule } from './db/db-mongo.module'; |
| 26 | import { HttpExceptionFilter } from './filters/http-exception.filter'; |
| 27 | import { LoggingInterceptor } from './interceptor/logging.interceptor'; |
| 28 | import { TransformInterceptor } from './interceptor/transform.interceptor'; |
| 29 | import { OssModule } from './lib/oss/oss.module'; |
| 30 | import { RedisModule } from './lib/redis/redis.module'; |
| 31 | import { AlicloudSmsModule } from './lib/sms/alicloud-sms.module'; |
| 32 | import { WxModule } from './lib/wx/wx.module'; |
| 33 | import { ManagerModule } from './manager/manager.module'; |
| 34 | import { TaskModule } from './modules/task/task.module'; |
| 35 | import { UserModule } from './user/user.module'; |
| 36 | import { XMLMiddleware } from './middleware/xml.middleware'; |
| 37 | import { FinanceModule } from './modules/finance/finance.module'; |
| 38 | import { OtherModule } from './modules/other/other.module'; |
| 39 | import { OperateModule } from './modules/operate/operate.module'; |
| 40 | import { ToolsModule } from './modules/tools/tools.module'; |
| 41 | import { AccountModule } from './modules/account/account.module'; |
| 42 | import { PublishModule } from './modules/publish/publish.module'; |
| 43 | import { TracingModule } from './modules/tracing/tracing.module'; |
| 44 | import { RewardModule } from './modules/reward/reward.module'; |
| 45 | import { TmsModule } from './lib/tms/tms.module'; |
| 46 | import { PlatModule } from './modules/plat/plat.module'; |
| 47 | import { BullModule } from '@nestjs/bullmq'; |
| 48 | import { MailModule } from './lib/mail/mail.module'; |
| 49 | |
| 50 | @Module({ |
| 51 | imports: [ |
| 52 | ConfigModule.forRoot({ |
| 53 | envFilePath: `.env${process.env.NODE_ENV ? '.' + process.env.NODE_ENV : ''}`, |
| 54 | isGlobal: true, |
| 55 | ignoreEnvFile: false, // 取消忽略配置文件,为true则仅读取操作系统环境变量,常用于生产环境 |
| 56 | load: [serverConfig, bullMqConfig, googleConfig], // 加载server.config自定义全局配置项 |
| 57 | validationSchema: Joi.object({ |
| 58 | // 配置文件.env校验 |
| 59 | PORT: Joi.string().default('7000'), |
| 60 | NODE_ENV: Joi.string() |
| 61 | .valid('development', 'production', 'test') |
| 62 | .default('development'), |
| 63 | }), |
| 64 | }), |
| 65 | EventEmitterModule.forRoot(), |
| 66 | ScheduleModule.forRoot(), |
| 67 | DbMongoModule, |
| 68 | RedisModule, |
| 69 | // 队列 |
| 70 | BullModule.forRootAsync({ |
| 71 | imports: [ConfigModule], |
| 72 | inject: [ConfigService], |
| 73 | useFactory: async (configService: ConfigService) => { |
| 74 | return { |
| 75 | connection: { |
| 76 | host: configService.get<string>('BULLMQ_REDIS_CONFIG.HOST'), |
| 77 | port: configService.get<number>('BULLMQ_REDIS_CONFIG.PORT'), |
| 78 | password: configService.get<string>('BULLMQ_REDIS_CONFIG.PASSWORD'), |
| 79 | db: configService.get<number>('BULLMQ_REDIS_CONFIG.DB'), |
| 80 | removeOnComplete: true, // 完成后删除 |
| 81 | removeOnFail: { count: 1, age: 1000 * 60 * 60 * 3 }, // 失败后保留一条记录一小时 |
| 82 | }, |
| 83 | }; |
| 84 | }, |
| 85 | }), |
| 86 | MailModule, |
| 87 | OssModule, |
| 88 | TmsModule, |
| 89 | AlicloudSmsModule, |
| 90 | AuthModule, |
| 91 | UserModule, |
| 92 | WxModule, |
| 93 | ManagerModule, |
| 94 | TaskModule, |
| 95 | FinanceModule, |
| 96 | OtherModule, |
| 97 | OperateModule, |
| 98 | ToolsModule, |
| 99 | AccountModule, |
| 100 | PublishModule, |
| 101 | TracingModule, |
| 102 | RewardModule, |
| 103 | PlatModule, |
| 104 | ], |
| 105 | controllers: [AppController], |
| 106 | providers: [ |
| 107 | AppService, |
| 108 | { provide: APP_FILTER, useClass: HttpExceptionFilter }, |
| 109 | { provide: APP_INTERCEPTOR, useClass: TransformInterceptor }, |
| 110 | { provide: APP_INTERCEPTOR, useClass: LoggingInterceptor }, |
| 111 | ], |
| 112 | }) |
| 113 | export class AppModule implements NestModule { |
| 114 | configure(consumer: MiddlewareConsumer) { |
| 115 | consumer.apply(XMLMiddleware).forRoutes({ |
| 116 | path: 'wxGzh/*', |
| 117 | method: RequestMethod.POST, |
| 118 | }); |
| 119 | } |
| 120 | } |
| 121 |