import { NextRequest, NextResponse } from 'next/server'; import { getSetting, setSetting } from '@/lib/db'; import { scheduleBackupPush } from '@/lib/backup-sync'; import { getRequestUserAgent } from '@/lib/request-user-agent'; const VALID_KEYS = ['max_concurrent_accounts', 'tick_interval_seconds', 'master_dashboard_url', 'instance_name', 'trading_hours', 'twocaptcha_api_key', 'twocaptcha_proxy', 'master_backup_token', 'proxy_url'] as const; type SettingKey = typeof VALID_KEYS[number]; export async function GET() { const result: Record = {}; for (const key of VALID_KEYS) { result[key] = getSetting(key); } // Read-only on purpose (not in VALID_KEYS): the master issues this uuid, // and editing it by hand would detach the instance from its person. result.instance_id = getSetting('instance_id'); // Generated once per instance. Callers cannot rotate an active identity. result.user_agent = getRequestUserAgent(); result.backup_last_push_at = getSetting('backup_last_push_at'); result.backup_last_error = getSetting('backup_last_error'); return NextResponse.json(result); } export async function PATCH(req: NextRequest) { try { const body = await req.json() as Partial>; for (const key of VALID_KEYS) { if (key in body) { const raw = body[key]; if (raw === undefined || raw === null) continue; setSetting(key, String(raw)); } } // The edit should reach the master's backup within seconds, not at the // next poll. scheduleBackupPush(); return NextResponse.json({ ok: true }); } catch (err: any) { return NextResponse.json({ error: err?.message ?? 'Failed to save settings' }, { status: 500 }); } }