49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
|
|
import fs from "fs";
|
||
|
|
import path from "path";
|
||
|
|
|
||
|
|
export type Settings = {
|
||
|
|
replicateApiToken: string;
|
||
|
|
jigsawApiKey: string;
|
||
|
|
modelVersion: string;
|
||
|
|
replicateEnabled: boolean;
|
||
|
|
adminPasswordHash: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
const DEFAULT_SETTINGS: Settings = {
|
||
|
|
replicateApiToken: process.env["REPLICATE_API_TOKEN"] ?? "",
|
||
|
|
jigsawApiKey: process.env["JIGSAWSTACK_API_KEY"] ?? "",
|
||
|
|
modelVersion: "jigsawstack/text-translate:454df4c49941c05dea05175bd37686d0872c73c1f9366d1c2505db32ade52a89",
|
||
|
|
replicateEnabled: false,
|
||
|
|
adminPasswordHash: process.env["ADMIN_PASSWORD"] ?? "admin"
|
||
|
|
};
|
||
|
|
|
||
|
|
const SETTINGS_PATH = path.join(process.cwd(), "data", "settings.json");
|
||
|
|
|
||
|
|
function ensureDataDir() {
|
||
|
|
const dir = path.dirname(SETTINGS_PATH);
|
||
|
|
if (!fs.existsSync(dir)) {
|
||
|
|
fs.mkdirSync(dir, { recursive: true });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function readSettings(): Settings {
|
||
|
|
try {
|
||
|
|
ensureDataDir();
|
||
|
|
if (!fs.existsSync(SETTINGS_PATH)) {
|
||
|
|
return { ...DEFAULT_SETTINGS };
|
||
|
|
}
|
||
|
|
const raw = fs.readFileSync(SETTINGS_PATH, "utf-8");
|
||
|
|
return { ...DEFAULT_SETTINGS, ...JSON.parse(raw) };
|
||
|
|
} catch {
|
||
|
|
return { ...DEFAULT_SETTINGS };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function writeSettings(updates: Partial<Settings>): Settings {
|
||
|
|
ensureDataDir();
|
||
|
|
const current = readSettings();
|
||
|
|
const next = { ...current, ...updates };
|
||
|
|
fs.writeFileSync(SETTINGS_PATH, JSON.stringify(next, null, 2), "utf-8");
|
||
|
|
return next;
|
||
|
|
}
|