2026-04-18 13:42:08 -04:00
|
|
|
import express from 'express';
|
|
|
|
|
import pool from '../db/pool.js';
|
|
|
|
|
import { requireAuth } from '../middleware/auth.js';
|
|
|
|
|
import { getAmppConfig } from '../ampp/client.js';
|
|
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
router.use(requireAuth);
|
|
|
|
|
|
2026-05-20 14:18:43 -04:00
|
|
|
// ── AMPP integration ───────────────────────────────────────────────────────
|
|
|
|
|
|
2026-04-18 13:42:08 -04:00
|
|
|
router.get('/ampp', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await pool.query(
|
|
|
|
|
"SELECT key, value FROM settings WHERE key IN ('ampp_base_url', 'ampp_token')"
|
|
|
|
|
);
|
|
|
|
|
const out = {};
|
|
|
|
|
for (const row of result.rows) {
|
|
|
|
|
if (row.key === 'ampp_token') {
|
2026-05-20 14:18:43 -04:00
|
|
|
out.ampp_token_exists = true;
|
2026-04-18 13:42:08 -04:00
|
|
|
} else {
|
|
|
|
|
out[row.key] = row.value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res.json(out);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.put('/ampp', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const { ampp_base_url, ampp_token } = req.body;
|
2026-05-20 14:18:43 -04:00
|
|
|
if (!ampp_base_url) return res.status(400).json({ error: 'ampp_base_url is required' });
|
2026-04-18 13:42:08 -04:00
|
|
|
const baseUrl = ampp_base_url.trim().replace(/\/$/, '');
|
|
|
|
|
await pool.query(
|
2026-05-20 14:18:43 -04:00
|
|
|
`INSERT INTO settings (key, value, updated_at) VALUES ('ampp_base_url', $1, NOW())
|
2026-04-18 13:42:08 -04:00
|
|
|
ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW()`,
|
|
|
|
|
[baseUrl]
|
|
|
|
|
);
|
|
|
|
|
if (ampp_token) {
|
|
|
|
|
await pool.query(
|
2026-05-20 14:18:43 -04:00
|
|
|
`INSERT INTO settings (key, value, updated_at) VALUES ('ampp_token', $1, NOW())
|
2026-04-18 13:42:08 -04:00
|
|
|
ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW()`,
|
|
|
|
|
[ampp_token.trim()]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
res.json({ message: 'AMPP settings saved' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/ampp/test', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const config = await getAmppConfig();
|
2026-05-20 14:18:43 -04:00
|
|
|
if (!config) return res.status(400).json({ error: 'AMPP credentials not configured' });
|
2026-04-18 13:42:08 -04:00
|
|
|
const testUrl = `${config.ampp_base_url}/api/v1/store/folder/folders?limit=1`;
|
|
|
|
|
const testRes = await fetch(testUrl, {
|
2026-05-20 14:18:43 -04:00
|
|
|
headers: { Authorization: `Bearer ${config.ampp_token}`, 'Content-Type': 'application/json' },
|
2026-04-18 13:42:08 -04:00
|
|
|
});
|
2026-05-20 14:18:43 -04:00
|
|
|
if (!testRes.ok) return res.status(400).json({ error: `AMPP returned HTTP ${testRes.status}` });
|
|
|
|
|
res.json({ message: 'AMPP connection successful' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(400).json({ error: `Connection failed: ${err.message}` });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ── Hardware inventory ─────────────────────────────────────────────────────
|
|
|
|
|
// GET /api/v1/settings/hardware — aggregate GPU/BMD capabilities from all cluster nodes
|
|
|
|
|
|
|
|
|
|
router.get('/hardware', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await pool.query(
|
|
|
|
|
`SELECT id, hostname, role, ip_address, capabilities,
|
|
|
|
|
EXTRACT(EPOCH FROM (NOW() - last_seen))::int AS stale_seconds
|
|
|
|
|
FROM cluster_nodes
|
|
|
|
|
ORDER BY role, hostname`
|
|
|
|
|
);
|
|
|
|
|
const nodes = result.rows.map(n => ({
|
|
|
|
|
id: n.id,
|
|
|
|
|
hostname: n.hostname,
|
|
|
|
|
role: n.role,
|
|
|
|
|
ip_address: n.ip_address,
|
|
|
|
|
online: n.stale_seconds < 120,
|
|
|
|
|
capabilities: n.capabilities || {},
|
|
|
|
|
}));
|
|
|
|
|
res.json({ nodes });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ── GPU/Transcoding settings ───────────────────────────────────────────────
|
|
|
|
|
// Keys: gpu_transcode_enabled, gpu_codec, gpu_preset, gpu_bitrate_mbps, gpu_node
|
|
|
|
|
|
|
|
|
|
router.get('/transcoding', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await pool.query(
|
|
|
|
|
`SELECT key, value FROM settings
|
|
|
|
|
WHERE key IN ('gpu_transcode_enabled','gpu_codec','gpu_preset','gpu_bitrate_mbps','gpu_node')`
|
|
|
|
|
);
|
|
|
|
|
const out = {
|
|
|
|
|
gpu_transcode_enabled: 'false',
|
|
|
|
|
gpu_codec: 'h264_nvenc',
|
|
|
|
|
gpu_preset: 'p4',
|
|
|
|
|
gpu_bitrate_mbps: '8',
|
|
|
|
|
gpu_node: '',
|
|
|
|
|
};
|
|
|
|
|
for (const { key, value } of result.rows) out[key] = value;
|
|
|
|
|
res.json(out);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-04-18 13:42:08 -04:00
|
|
|
|
2026-05-20 14:18:43 -04:00
|
|
|
router.put('/transcoding', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const allowed = ['gpu_transcode_enabled', 'gpu_codec', 'gpu_preset', 'gpu_bitrate_mbps', 'gpu_node'];
|
|
|
|
|
for (const key of allowed) {
|
|
|
|
|
if (req.body[key] !== undefined) {
|
|
|
|
|
await pool.query(
|
|
|
|
|
`INSERT INTO settings (key, value, updated_at) VALUES ($1, $2, NOW())
|
|
|
|
|
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()`,
|
|
|
|
|
[key, String(req.body[key])]
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-18 13:42:08 -04:00
|
|
|
}
|
2026-05-20 14:18:43 -04:00
|
|
|
res.json({ message: 'Transcoding settings saved' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-04-18 13:42:08 -04:00
|
|
|
|
2026-05-20 14:18:43 -04:00
|
|
|
// ── Capture service routing ────────────────────────────────────────────────
|
|
|
|
|
// capture_service_url — points to the remote capture node's API
|
|
|
|
|
// When set, the local capture proxy routes all requests to this URL instead of the sidecar
|
|
|
|
|
|
|
|
|
|
router.get('/capture-service', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await pool.query(
|
|
|
|
|
"SELECT value FROM settings WHERE key = 'capture_service_url'"
|
|
|
|
|
);
|
|
|
|
|
res.json({ capture_service_url: result.rows[0]?.value || '' });
|
2026-04-18 13:42:08 -04:00
|
|
|
} catch (err) {
|
2026-05-20 14:18:43 -04:00
|
|
|
next(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.put('/capture-service', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const url = (req.body.capture_service_url || '').trim().replace(/\/$/, '');
|
|
|
|
|
await pool.query(
|
|
|
|
|
`INSERT INTO settings (key, value, updated_at) VALUES ('capture_service_url', $1, NOW())
|
|
|
|
|
ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW()`,
|
|
|
|
|
[url]
|
|
|
|
|
);
|
|
|
|
|
res.json({ message: 'Capture service URL saved' });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
next(err);
|
2026-04-18 13:42:08 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default router;
|