115 lines
3.4 KiB
TypeScript
115 lines
3.4 KiB
TypeScript
|
|
import { Controller, Get, UseGuards } from '@nestjs/common';
|
|||
|
|
import { ConfigService } from '@nestjs/config';
|
|||
|
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
|||
|
|
import { Public } from '../auth/decorators/public.decorator';
|
|||
|
|
import * as fs from 'fs';
|
|||
|
|
import * as path from 'path';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 配置验证控制器
|
|||
|
|
* 用于验证环境配置文件是否正确加载
|
|||
|
|
*/
|
|||
|
|
@Controller('config-verification')
|
|||
|
|
export class ConfigVerificationController {
|
|||
|
|
constructor(private configService: ConfigService) {}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 公开接口,用于验证配置加载
|
|||
|
|
*/
|
|||
|
|
@Public()
|
|||
|
|
@Get('env-info')
|
|||
|
|
getEnvInfo() {
|
|||
|
|
const nodeEnv = process.env.NODE_ENV || 'development';
|
|||
|
|
const expectedEnvFile = `.env.${nodeEnv}`; // 匹配实际文件名格式:.development.env
|
|||
|
|
const envFilePath = path.join(process.cwd(), expectedEnvFile);
|
|||
|
|
const fallbackEnvPath = path.join(process.cwd(), '.env');
|
|||
|
|
|
|||
|
|
// 检查文件是否存在
|
|||
|
|
const envFileExists = fs.existsSync(envFilePath);
|
|||
|
|
const fallbackExists = fs.existsSync(fallbackEnvPath);
|
|||
|
|
|
|||
|
|
// 获取一些关键配置(不暴露敏感信息)
|
|||
|
|
const config = {
|
|||
|
|
nodeEnv,
|
|||
|
|
expectedEnvFile,
|
|||
|
|
envFileExists,
|
|||
|
|
fallbackExists,
|
|||
|
|
envFilePath,
|
|||
|
|
fallbackEnvPath,
|
|||
|
|
loadedFrom: envFileExists
|
|||
|
|
? expectedEnvFile
|
|||
|
|
: fallbackExists
|
|||
|
|
? '.env'
|
|||
|
|
: '环境变量',
|
|||
|
|
// 显示具体配置信息(包括实际值)
|
|||
|
|
configs: {
|
|||
|
|
PORT: this.configService.get('PORT') || process.env.PORT || 3001,
|
|||
|
|
DATABASE_URL:
|
|||
|
|
this.configService.get('DATABASE_URL') ||
|
|||
|
|
process.env.DATABASE_URL ||
|
|||
|
|
'未配置',
|
|||
|
|
JWT_SECRET:
|
|||
|
|
this.configService.get('JWT_SECRET') ||
|
|||
|
|
process.env.JWT_SECRET ||
|
|||
|
|
'未配置',
|
|||
|
|
NODE_ENV: this.configService.get('NODE_ENV') || nodeEnv,
|
|||
|
|
},
|
|||
|
|
publicConfigs: {
|
|||
|
|
PORT: this.configService.get('PORT') || process.env.PORT || 3001,
|
|||
|
|
NODE_ENV: this.configService.get('NODE_ENV') || nodeEnv,
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
code: 200,
|
|||
|
|
message: '配置信息',
|
|||
|
|
data: config,
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 需要认证的接口,显示更多配置详情(仍隐藏敏感信息)
|
|||
|
|
*/
|
|||
|
|
@Get('detailed')
|
|||
|
|
getDetailedConfig() {
|
|||
|
|
const nodeEnv = process.env.NODE_ENV || 'development';
|
|||
|
|
const expectedEnvFile = `.env.${nodeEnv}`;
|
|||
|
|
const envFilePath = path.join(process.cwd(), expectedEnvFile);
|
|||
|
|
|
|||
|
|
// 读取文件内容(用于验证,但不返回敏感信息)
|
|||
|
|
let fileContent = '';
|
|||
|
|
try {
|
|||
|
|
if (fs.existsSync(envFilePath)) {
|
|||
|
|
fileContent = fs.readFileSync(envFilePath, 'utf-8');
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
// 忽略读取错误
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 统计配置项数量
|
|||
|
|
const configKeys = fileContent
|
|||
|
|
.split('\n')
|
|||
|
|
.filter((line) => line.trim() && !line.trim().startsWith('#'))
|
|||
|
|
.map((line) => line.split('=')[0]?.trim())
|
|||
|
|
.filter(Boolean);
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
code: 200,
|
|||
|
|
message: '详细配置信息',
|
|||
|
|
data: {
|
|||
|
|
nodeEnv,
|
|||
|
|
expectedEnvFile,
|
|||
|
|
fileExists: fs.existsSync(envFilePath),
|
|||
|
|
configKeysCount: configKeys.length,
|
|||
|
|
configKeys: configKeys, // 只显示键名,不显示值
|
|||
|
|
// 验证关键配置是否加载
|
|||
|
|
verification: {
|
|||
|
|
DATABASE_URL: !!this.configService.get('DATABASE_URL'),
|
|||
|
|
JWT_SECRET: !!this.configService.get('JWT_SECRET'),
|
|||
|
|
PORT: !!this.configService.get('PORT'),
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|