64 lines
2.7 KiB
TypeScript
64 lines
2.7 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { requireAdmin } from "@/src/lib/auth";
|
|
import { applyCaddyConfig } from "@/src/lib/caddy";
|
|
import { getCloudflareSettings, saveCloudflareSettings, saveGeneralSettings } from "@/src/lib/settings";
|
|
|
|
type ActionResult = {
|
|
success: boolean;
|
|
message?: string;
|
|
};
|
|
|
|
export async function updateGeneralSettingsAction(_prevState: ActionResult | null, formData: FormData): Promise<ActionResult> {
|
|
try {
|
|
await requireAdmin();
|
|
await saveGeneralSettings({
|
|
primaryDomain: String(formData.get("primaryDomain") ?? ""),
|
|
acmeEmail: formData.get("acmeEmail") ? String(formData.get("acmeEmail")) : undefined
|
|
});
|
|
revalidatePath("/settings");
|
|
return { success: true, message: "General settings saved successfully" };
|
|
} catch (error) {
|
|
console.error("Failed to save general settings:", error);
|
|
return { success: false, message: error instanceof Error ? error.message : "Failed to save general settings" };
|
|
}
|
|
}
|
|
|
|
export async function updateCloudflareSettingsAction(_prevState: ActionResult | null, formData: FormData): Promise<ActionResult> {
|
|
try {
|
|
await requireAdmin();
|
|
const rawToken = formData.get("apiToken") ? String(formData.get("apiToken")).trim() : "";
|
|
const clearToken = formData.get("clearToken") === "on";
|
|
const current = await getCloudflareSettings();
|
|
|
|
const apiToken = clearToken ? "" : rawToken || current?.apiToken || "";
|
|
const zoneId = formData.get("zoneId") ? String(formData.get("zoneId")) : undefined;
|
|
const accountId = formData.get("accountId") ? String(formData.get("accountId")) : undefined;
|
|
|
|
await saveCloudflareSettings({
|
|
apiToken,
|
|
zoneId: zoneId && zoneId.length > 0 ? zoneId : undefined,
|
|
accountId: accountId && accountId.length > 0 ? accountId : undefined
|
|
});
|
|
|
|
// Try to apply the config, but don't fail if Caddy is unreachable
|
|
try {
|
|
await applyCaddyConfig();
|
|
revalidatePath("/settings");
|
|
return { success: true, message: "Cloudflare settings saved and applied to Caddy successfully" };
|
|
} catch (error) {
|
|
console.error("Failed to apply Caddy config:", error);
|
|
revalidatePath("/settings");
|
|
const errorMsg = error instanceof Error ? error.message : "Unknown error";
|
|
return {
|
|
success: true, // Settings were saved successfully
|
|
message: `Settings saved, but could not apply to Caddy: ${errorMsg}. You may need to start Caddy or check your configuration.`
|
|
};
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to save Cloudflare settings:", error);
|
|
return { success: false, message: error instanceof Error ? error.message : "Failed to save Cloudflare settings" };
|
|
}
|
|
}
|