feat(logging): add logging middleware for HTTP requests and update API timeout

- Introduce LoggingMiddleware to log incoming requests and their response times
- Update AppModule to apply the new logging middleware globally
- Increase API request timeout from 10 seconds to 60 seconds
- Modify PromptString to include additional project types of interest
This commit is contained in:
dmy
2026-01-15 21:52:40 +08:00
parent 3033eb622f
commit 811ad927f3
5 changed files with 39 additions and 4 deletions

View File

@@ -6,7 +6,7 @@ import axios from 'axios'
*/
const api = axios.create({
baseURL: 'http://localhost:3000', // 设置后端服务地址
timeout: 10000, // 请求超时时间
timeout: 60000, // 请求超时时间60秒
})
// 请求拦截器

View File

@@ -1 +1 @@
export const PromptString: string = `先给我说你统计了多少个项目。我只对辽宁、山东、江苏、浙江、福建、广东、广西、海南、河北这些地方的海上风电、海上光伏、漂浮式光伏、滩涂光伏、滩涂风电、渔光互补项目感兴趣。从我提供的这些工程里面找到我感兴趣的工程无论如何至少推荐10个工程。如果没有推荐的也要给出思考过程。`;
export const PromptString: string = `先给我说你统计了多少个项目。我只对辽宁、山东、江苏、浙江、福建、广东、广西、海南、河北这些地方的海上风电、海上光伏、漂浮式光伏、滩涂光伏、滩涂风电、渔光互补、风光互补项目感兴趣。从我提供的这些工程里面找到我感兴趣的工程无论如何至少推荐10个工程。如果没有推荐的也要给出思考过程。`;

View File

@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ScheduleModule } from '@nestjs/schedule';
import { ServeStaticModule } from '@nestjs/serve-static';
@@ -9,6 +9,7 @@ import { KeywordsModule } from './keywords/keywords.module';
import { CrawlerModule } from './crawler/crawler.module';
import { TasksModule } from './schedule/schedule.module';
import { LoggerModule } from './common/logger/logger.module';
import { LoggingMiddleware } from './common/logger/logging.middleware';
import { AiModule } from './ai/ai.module';
@Module({
@@ -28,4 +29,8 @@ import { AiModule } from './ai/ai.module';
AiModule,
],
})
export class AppModule {}
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggingMiddleware).forRoutes('*');
}
}

View File

@@ -0,0 +1,29 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { CustomLogger } from './logger.service';
@Injectable()
export class LoggingMiddleware implements NestMiddleware {
constructor(private readonly logger: CustomLogger) {
this.logger.setContext('HTTP');
}
use(req: Request, res: Response, next: NextFunction) {
const { method, originalUrl, ip } = req;
const userAgent = req.get('user-agent') || '';
const startTime = Date.now();
// 收到请求时立即输出
this.logger.debug(`--> ${method} ${originalUrl} - ${ip} - ${userAgent}`);
res.on('finish', () => {
const { statusCode } = res;
const duration = Date.now() - startTime;
this.logger.debug(
`<-- ${method} ${originalUrl} ${statusCode} - ${duration}ms`,
);
});
next();
}
}

View File

@@ -1,3 +1,4 @@
import 'dotenv/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CustomLogger } from './common/logger/logger.service';