67 lines
3.1 KiB
JavaScript
67 lines
3.1 KiB
JavaScript
'use strict';
|
||
|
||
const express = require('express');
|
||
const http = require('http');
|
||
const path = require('path');
|
||
const { createProxyMiddleware } = require('http-proxy-middleware');
|
||
const { createSnapshotRouter } = require('./src/snapshotService');
|
||
|
||
const PORT = parseInt(process.env.PORT ?? '8444', 10);
|
||
const GO2RTC_URL = process.env.GO2RTC_URL ?? 'http://localhost:1984';
|
||
|
||
const app = express();
|
||
|
||
// ── Stabile Snapshot-API (vor dem Proxy registrieren!) ────────────────────────
|
||
// Für das Homing-Projekt: GET /api/snapshot/cam0 → JPEG
|
||
app.use('/api/snapshot', createSnapshotRouter(GO2RTC_URL));
|
||
|
||
// ── Health ────────────────────────────────────────────────────────────────────
|
||
app.get('/health', async (_req, res) => {
|
||
try {
|
||
const r = await fetch(`${GO2RTC_URL}/api/streams`);
|
||
const streams = r.ok ? await r.json() : {};
|
||
res.json({ status: r.ok ? 'ok' : 'degraded', cameras: Object.keys(streams) });
|
||
} catch (err) {
|
||
res.status(503).json({ status: 'down', error: err.message });
|
||
}
|
||
});
|
||
|
||
// ── Reverse-Proxy zu go2rtc ───────────────────────────────────────────────────
|
||
// Reicht nur die nötigen Pfade durch (go2rtc-Admin bleibt damit unerreichbar):
|
||
// /api/ws WebRTC/MSE-Signaling (WebSocket)
|
||
// /api/frame.jpeg Snapshots
|
||
// /api/stream.* MJPEG/MP4-Fallback
|
||
// /api/streams Stream-Liste
|
||
// /video-rtc.js, /video-stream.js go2rtc's offizieller Player
|
||
const go2rtcProxy = createProxyMiddleware({
|
||
target: GO2RTC_URL,
|
||
changeOrigin: true,
|
||
ws: true,
|
||
// Plain-Pfade: dürfen NICHT mit Globs (/api/**) gemischt werden (HPM v3)
|
||
// '/api' matcht alles ab /api/... — kein ** nötig
|
||
pathFilter: ['/api', '/video-rtc.js', '/video-stream.js'],
|
||
logger: console,
|
||
});
|
||
app.use(go2rtcProxy);
|
||
|
||
// ── Eigener Viewer ─────────────────────────────────────────────────────────────
|
||
app.use(express.static(path.join(__dirname, 'public')));
|
||
|
||
// ── Start ───────────────────────────────────────────────────────────────────────
|
||
const server = http.createServer(app);
|
||
server.on('upgrade', go2rtcProxy.upgrade); // WebSocket-Signaling durchreichen
|
||
|
||
server.listen(PORT, '0.0.0.0', () => {
|
||
console.log(`AppRobotWebcam on http://0.0.0.0:${PORT}`);
|
||
console.log(` go2rtc backend: ${GO2RTC_URL}`);
|
||
console.log(` Viewer: http://0.0.0.0:${PORT}/`);
|
||
console.log(` Snapshot (API): http://0.0.0.0:${PORT}/api/snapshot/cam0`);
|
||
});
|
||
|
||
const shutdown = (sig) => {
|
||
console.log(`\n${sig} – shutting down`);
|
||
server.close(() => process.exit(0));
|
||
};
|
||
process.on('SIGINT', () => shutdown('SIGINT'));
|
||
process.on('SIGTERM', () => shutdown('SIGTERM'));
|