Claude: Fix
This commit is contained in:
55
server.js
55
server.js
@@ -6,16 +6,17 @@ 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 PORT = parseInt(process.env.PORT ?? '8444', 10);
|
||||
const GO2RTC_URL = process.env.GO2RTC_URL ?? 'http://localhost:1984';
|
||||
const GO2RTC_PORT = parseInt(process.env.GO2RTC_PORT ?? '1984', 10);
|
||||
|
||||
const app = express();
|
||||
|
||||
// ── Stabile Snapshot-API (vor dem Proxy registrieren!) ────────────────────────
|
||||
// Für das Homing-Projekt: GET /api/snapshot/cam0 → JPEG
|
||||
// ── 1. Eigene Endpunkte (vor dem Proxy registrieren) ─────────────────────────
|
||||
|
||||
// Stabile Snapshot-API für das Homing-Projekt
|
||||
app.use('/api/snapshot', createSnapshotRouter(GO2RTC_URL));
|
||||
|
||||
// ── Health ────────────────────────────────────────────────────────────────────
|
||||
app.get('/health', async (_req, res) => {
|
||||
try {
|
||||
const r = await fetch(`${GO2RTC_URL}/api/streams`);
|
||||
@@ -26,36 +27,40 @@ app.get('/health', async (_req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ── 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
|
||||
// Gibt dem Viewer die go2rtc-Port-Nummer mit – Browser baut WS direkt zu go2rtc.
|
||||
// Trennung HTTP (Node) / WebSocket (go2rtc) ist sauberer als ein fragiler WS-Proxy.
|
||||
app.get('/config.json', (_req, res) => {
|
||||
res.json({ go2rtcPort: GO2RTC_PORT });
|
||||
});
|
||||
|
||||
// ── 2. HTTP-Proxy zu go2rtc (nur für Script-Dateien und API ohne WS) ─────────
|
||||
// /api/ws NICHT hier proxy-en – das macht der Browser direkt (s. viewer.js).
|
||||
const go2rtcProxy = createProxyMiddleware({
|
||||
target: GO2RTC_URL,
|
||||
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,
|
||||
pathFilter: ['/api', '/video-rtc.js', '/video-stream.js'],
|
||||
logger: console,
|
||||
on: {
|
||||
error: (err, req, res) => {
|
||||
console.error('[HPM] proxy error:', err.message);
|
||||
if (!res.headersSent) res.status(502).json({ error: 'go2rtc nicht erreichbar' });
|
||||
},
|
||||
},
|
||||
});
|
||||
app.use(go2rtcProxy);
|
||||
|
||||
// ── Eigener Viewer ─────────────────────────────────────────────────────────────
|
||||
// ── 3. Statische Dateien (eigener Viewer) ────────────────────────────────────
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
// ── Start ───────────────────────────────────────────────────────────────────────
|
||||
// ── 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`);
|
||||
console.log(`AppRobotWebcam http://0.0.0.0:${PORT}`);
|
||||
console.log(` go2rtc HTTP: ${GO2RTC_URL}`);
|
||||
console.log(` go2rtc WS: ws://[host]:${GO2RTC_PORT} (Browser verbindet direkt)`);
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user