Four features: - Backup: lib/backup-sync.ts pushes the full settings table and firm credentials (the deliberate exception to "credentials never leave the machine") to the master's bearer-gated /api/instances/backup — every 5 minutes when the content hash changed, within seconds of a settings or credential edit, and on the new backup-now command. Enabled by the master_backup_token setting. - Restore: a dead VPS's replacement pulls the backup and adopts its uuid/name, so roster, payouts and card links carry on. Pre-boot via seed-settings.js --restore-from (setup-linux.sh prompts for it) or the Settings page's restore picker (lib/backup-restore.ts). Stop the old machine first; the master 409s a live-name rename. - Proxy: the proxy_url setting routes BROKER traffic only — TopstepX REST + SignalR hub, Tradovate REST + websocket, Yahoo quotes — via lib/outbound-proxy.ts (axios agents, undici dispatcher, ws agent). The master link, reporter, catalog and backup push stay direct, so a dead proxy cannot take down the control channel. Restart to apply. - Multi-instance: AUTOFIRMER_DB moves the SQLite file so one checkout runs N instances; scripts/instances.json (per-device, gitignored) drives start-all/update-check, and add-instance.sh / remove-instance.sh manage the extra PM2 processes. Absent registry = single-instance behavior, unchanged. Instance 1 keeps port 3000 for the clicker and extension. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
57 lines
2.3 KiB
TypeScript
57 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getCachedFirm } from '@/lib/firm-catalog';
|
|
import { setCredentials, clearCredentials } from '@/lib/db';
|
|
import { reconcileClients, removeClient } from '@/lib/clients';
|
|
import { scheduleBackupPush } from '@/lib/backup-sync';
|
|
|
|
type Params = { params: Promise<{ id: string }> };
|
|
|
|
/** The platform login for one firm. This is the only firm data this machine
|
|
* owns — everything else comes from the master catalog. The one path these
|
|
* credentials travel is the encrypted, bearer-gated backup to the master
|
|
* (lib/backup-sync.ts); they are still absent from the catalog and the
|
|
* control socket.
|
|
*
|
|
* Tradovate firms need username + password. TopstepX firms need the API key;
|
|
* username is optional for now (the loginKey call still wants it, so the
|
|
* client sits idle with a clear error until one is added) and password is
|
|
* unused. */
|
|
export async function PUT(req: NextRequest, { params }: Params) {
|
|
const { id } = await params;
|
|
|
|
const firm = getCachedFirm(id);
|
|
if (!firm) return NextResponse.json({ error: 'Firm not found' }, { status: 404 });
|
|
|
|
const body = await req.json() as { username?: string; password?: string; apiKey?: string };
|
|
const username = body.username?.trim() ?? '';
|
|
const password = body.password?.trim() ?? '';
|
|
const apiKey = body.apiKey?.trim() ?? '';
|
|
|
|
if (firm.platform === 'topstepx') {
|
|
if (!apiKey) {
|
|
return NextResponse.json({ error: 'apiKey is required for a TopstepX firm' }, { status: 400 });
|
|
}
|
|
} else if (!username || !password) {
|
|
return NextResponse.json({ error: 'username and password are required' }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
setCredentials(id, firm.name, username, password, apiKey);
|
|
// Picks up the new login immediately rather than at the next restart.
|
|
reconcileClients();
|
|
scheduleBackupPush();
|
|
return NextResponse.json({ ok: true });
|
|
} catch (err) {
|
|
console.error('[PUT /api/firms/:id/credentials]', err);
|
|
return NextResponse.json({ error: 'Failed to save credentials' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_req: NextRequest, { params }: Params) {
|
|
const { id } = await params;
|
|
const cleared = clearCredentials(id);
|
|
removeClient(id);
|
|
scheduleBackupPush();
|
|
return NextResponse.json({ ok: cleared });
|
|
}
|