Files
system_update/server/services/appSettings.ts
T
gilles 2b684da9cd feat(api): profil machine éditable, sonde, et réglages globaux apt-cacher-ng
- machines : updateMachine (PATCH /machines/:id) + POST /machines/:id/probe
  (sonde synchrone → faits + proposition de correction) ; MachineView expose
  machineKind/virtualization ; CreateMachineInput accepte aptProxyMode persistent
- app_settings (clé/valeur, migration 0006) + service appSettings :
  défaut apt-cacher-ng (mode + url) ; applyProxyToAllMachines
- routes /settings : GET, PUT /apt-proxy, POST /apt-proxy/apply-all

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 07:53:47 +02:00

44 lines
1.3 KiB
TypeScript

// server/services/appSettings.ts
import { db, schema } from "../db/client.js";
import type { AptProxyMode } from "@shared/types.js";
export const SETTING_KEYS = {
defaultAptProxyUrl: "default_apt_proxy_url",
defaultAptProxyMode: "default_apt_proxy_mode",
} as const;
export function getAllSettings(): Record<string, string> {
return Object.fromEntries(
db.select().from(schema.appSettings).all().map((r) => [r.key, r.value ?? ""]),
);
}
export function setSettings(patch: Record<string, string | null>): void {
const now = new Date().toISOString();
for (const [key, value] of Object.entries(patch)) {
db.insert(schema.appSettings)
.values({ key, value, updatedAt: now })
.onConflictDoUpdate({ target: schema.appSettings.key, set: { value, updatedAt: now } })
.run();
}
}
export interface DefaultAptProxy {
mode: AptProxyMode;
url: string | null;
}
export function getDefaultAptProxy(): DefaultAptProxy {
const s = getAllSettings();
const mode = (s[SETTING_KEYS.defaultAptProxyMode] as AptProxyMode) || "direct";
return { mode, url: s[SETTING_KEYS.defaultAptProxyUrl] || null };
}
export function setDefaultAptProxy(input: DefaultAptProxy): DefaultAptProxy {
setSettings({
[SETTING_KEYS.defaultAptProxyMode]: input.mode,
[SETTING_KEYS.defaultAptProxyUrl]: input.url ?? "",
});
return getDefaultAptProxy();
}