更新安全配置项目
This commit is contained in:
+2
-2
@@ -1,5 +1,5 @@
|
||||
import OpenAI from "openai";
|
||||
import {getConfig} from "@/lib/config";
|
||||
import {getAIConfig} from "@/lib/config";
|
||||
|
||||
function parseTranslation(content: string): string | null {
|
||||
// 第一步:清理内容,移除 Markdown 代码块、换行、多余空格
|
||||
@@ -21,7 +21,7 @@ function parseTranslation(content: string): string | null {
|
||||
// 翻译单个词组(带重试)
|
||||
export async function translateWithAI(word: string): Promise<string | null> {
|
||||
// 获取配置
|
||||
const aiConfig = getConfig().ai
|
||||
const aiConfig = getAIConfig()
|
||||
|
||||
if (!aiConfig.apiKey) {
|
||||
throw new Error("AI API Key 未配置");
|
||||
|
||||
+1
-51
@@ -179,7 +179,7 @@ export function getExportConfig(): ExportConfig {
|
||||
/**
|
||||
* 获取完整配置
|
||||
*/
|
||||
export function getConfig(): AppConfig {
|
||||
export function getAllConfig(): AppConfig {
|
||||
// 如果配置文件不存在,从环境变量初始化
|
||||
if (!configFileExists()) {
|
||||
return initConfigFromEnv();
|
||||
@@ -192,37 +192,6 @@ export function getConfig(): AppConfig {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据库配置
|
||||
*/
|
||||
export function saveDBConfig(config: Partial<DBConfig>): DBConfig {
|
||||
// 确保配置文件存在
|
||||
if (!configFileExists()) {
|
||||
initConfigFromEnv();
|
||||
}
|
||||
|
||||
const current = getDBConfig();
|
||||
|
||||
const newConfig: DBConfig = {
|
||||
host: config.host ?? current.host,
|
||||
port: config.port ?? current.port,
|
||||
user: config.user ?? current.user,
|
||||
password: config.password ?? current.password,
|
||||
database: config.database ?? current.database,
|
||||
};
|
||||
|
||||
// 读取现有配置,更新 db 部分
|
||||
const fileConfig = readConfigFile();
|
||||
const updatedConfig: AppConfig = {
|
||||
ai: fileConfig.ai ?? { apiKey: "", baseUrl: "https://api.openai.com/v1", model: "gpt-3.5-turbo" },
|
||||
export: fileConfig.export ?? { untranslatedColor: "CC0000", secondPageHeader: false, orientation: "landscape" },
|
||||
db: newConfig,
|
||||
};
|
||||
writeConfigFile(updatedConfig);
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存AI翻译配置
|
||||
*/
|
||||
@@ -280,22 +249,3 @@ export function saveExportConfig(config: Partial<ExportConfig>): ExportConfig {
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存完整配置
|
||||
*/
|
||||
export function saveConfig(config: Partial<AppConfig>): AppConfig {
|
||||
// 确保配置文件存在
|
||||
if (!configFileExists()) {
|
||||
initConfigFromEnv();
|
||||
}
|
||||
|
||||
const current = getConfig();
|
||||
const fileConfig = readConfigFile();
|
||||
|
||||
return {
|
||||
db: config.db ? saveDBConfig(config.db) : current.db,
|
||||
ai: config.ai ? saveAIConfig(config.ai) : current.ai,
|
||||
export: config.export ? saveExportConfig(config.export) : current.export,
|
||||
};
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
import mysql from "mysql2/promise";
|
||||
import {getConfig} from "@/lib/config";
|
||||
import {getDBConfig} from "@/lib/config";
|
||||
|
||||
// 获取配置文件中的数据链接
|
||||
const dataBaseConfig = getConfig().db
|
||||
// 获取配置文件中的数据
|
||||
const dataBaseConfig = getDBConfig()
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: dataBaseConfig.host || "127.0.0.1",
|
||||
|
||||
Reference in New Issue
Block a user