mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-10-14 13:29:19 +00:00
Compare commits
No commits in common. "83fe8d5e45b26fcbd960740cd457533800754a6d" and "4310e94547d356db6fc1f63aca2380ac27747734" have entirely different histories.
83fe8d5e45
...
4310e94547
@ -4,7 +4,4 @@ WS_SECRET=114514
|
||||
TOKEN=54188
|
||||
OPENLIST_API_BASE_URL=http://127.0.0.1:5244
|
||||
OPENLIST_API_BASE_USERNAME=USER
|
||||
OPENLIST_API_BASE_PASSWORD=123456
|
||||
OPENLIST_API_MEME_PATH=/crystelf/meme
|
||||
OPENLIST_API_CDN_PATH=/crystelf/cdn
|
||||
|
||||
OPENLIST_API_BASE_PASSWORD=123456
|
@ -1,13 +1,9 @@
|
||||
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService as NestConfigService } from '@nestjs/config';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
@Injectable()
|
||||
export class AppConfigService implements OnModuleInit {
|
||||
private readonly logger = new Logger(AppConfigService.name);
|
||||
private readonly envPath = path.resolve(process.cwd(), '.env');
|
||||
private readonly envExamplePath = path.resolve(process.cwd(), '.envExample');
|
||||
|
||||
constructor(
|
||||
@Inject(NestConfigService)
|
||||
@ -15,11 +11,13 @@ export class AppConfigService implements OnModuleInit {
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
this.checkAndSyncEnv();
|
||||
this.checkRequiredVariables();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取环境变量
|
||||
* @param key 键值
|
||||
* @param defaultValue 默认
|
||||
*/
|
||||
public get<T = string>(key: string, defaultValue?: T): T | undefined {
|
||||
const value = this.nestConfigService.get<T>(key);
|
||||
@ -32,60 +30,17 @@ export class AppConfigService implements OnModuleInit {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查并补全 .env 文件
|
||||
*/
|
||||
private checkAndSyncEnv(): void {
|
||||
this.logger.log('检查并同步 .env 与 .env.example ...');
|
||||
private checkRequiredVariables(): void {
|
||||
this.logger.log('检查必要环境变量..');
|
||||
const requiredVariables = ['RD_PORT', 'RD_ADD', 'WS_SECRET'];
|
||||
|
||||
if (!fs.existsSync(this.envExamplePath)) {
|
||||
this.logger.error(`缺少 ${this.envExamplePath} 文件,无法校验`);
|
||||
return;
|
||||
}
|
||||
|
||||
const exampleContent = fs.readFileSync(this.envExamplePath, 'utf8');
|
||||
const exampleVars = this.parseEnv(exampleContent);
|
||||
|
||||
let envContent = fs.existsSync(this.envPath)
|
||||
? fs.readFileSync(this.envPath, 'utf8')
|
||||
: '';
|
||||
const envVars = this.parseEnv(envContent);
|
||||
|
||||
let updated = false;
|
||||
for (const key of Object.keys(exampleVars)) {
|
||||
if (!(key in envVars)) {
|
||||
const value = exampleVars[key] ?? '';
|
||||
envContent += `\n${key}=${value}`;
|
||||
this.logger.warn(`补全缺失环境变量: ${key}=${value}`);
|
||||
updated = true;
|
||||
requiredVariables.forEach((key) => {
|
||||
const value = this.nestConfigService.get(key);
|
||||
if (value === undefined || value === null) {
|
||||
this.logger.fatal(`必需环境变量缺失: ${key}`);
|
||||
} else {
|
||||
this.logger.debug(`检测到环境变量: ${key}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
fs.writeFileSync(this.envPath, envContent.trim() + '\n', {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
this.logger.log('.env 已自动补全缺失项');
|
||||
} else {
|
||||
this.logger.log('.env 已与 .env.example 保持一致');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析.env为对象
|
||||
* @param content
|
||||
* @private
|
||||
*/
|
||||
private parseEnv(content: string): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
content
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line && !line.startsWith('#'))
|
||||
.forEach((line) => {
|
||||
const [key, ...rest] = line.split('=');
|
||||
result[key.trim()] = rest.join('=').trim();
|
||||
});
|
||||
return result;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user