GitHub CoPilot Zertifikat

This commit is contained in:
chk
2026-06-08 19:08:13 +02:00
parent 71f227ace1
commit 90f60f9d5d
3 changed files with 61 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
import express from 'express';
import https from 'https';
import path from 'path';
import fs from 'fs/promises';
import fs from 'fs';
import fsPromises from 'fs/promises';
import { fileURLToPath } from 'url';
import process from 'process';
@@ -15,6 +17,9 @@ const publicDir = path.join(__dirname, '..', 'public');
const snapshotsDir = path.join(publicDir, 'snapshots');
const WEBCAM_URL = process.env.WEBCAM_URL || '';
const BODYTRACKER_URL = process.env.BODYTRACKER_URL || '';
const HTTPS_KEY_PATH = process.env.HTTPS_KEY_PATH || path.join(__dirname, '..', 'https', 'localhost.key');
const HTTPS_CERT_PATH = process.env.HTTPS_CERT_PATH || path.join(__dirname, '..', 'https', 'localhost.pem');
const HTTPS_PASSPHRASE = process.env.HTTPS_PASSPHRASE || 'abcd';
app.use(express.static(publicDir));
@@ -23,13 +28,13 @@ app.get('/api/health', (req, res) => {
});
async function findLatestSnapshotFile() {
const files = await fs.readdir(snapshotsDir);
const files = await fsPromises.readdir(snapshotsDir);
const entries = await Promise.all(
files
.filter((name) => name.endsWith('.csv'))
.map(async (name) => ({
name,
mtime: (await fs.stat(path.join(snapshotsDir, name))).mtime.valueOf()
mtime: (await fsPromises.stat(path.join(snapshotsDir, name))).mtime.valueOf()
}))
);
if (entries.length === 0) return null;
@@ -69,15 +74,15 @@ app.get('/api/latest-snapshot', async (req, res) => {
const imagePath = path.join(snapshotsDir, `${baseName}_annotated.jpg`);
const imagePath2 = path.join(snapshotsDir, `${baseName}_annotated2.jpg`);
const content = await fs.readFile(csvPath, 'utf8');
const result = { filename: latestFile, mtime: (await fs.stat(csvPath)).mtime.toISOString(), content };
const content = await fsPromises.readFile(csvPath, 'utf8');
const result = { filename: latestFile, mtime: (await fsPromises.stat(csvPath)).mtime.toISOString(), content };
try {
result.jsonFile = { filename: `${baseName}.json`, content: await fs.readFile(jsonPath, 'utf8') };
result.jsonFile = { filename: `${baseName}.json`, content: await fsPromises.readFile(jsonPath, 'utf8') };
} catch {}
try {
const jpg = await fs.readFile(imagePath);
const jpg = await fsPromises.readFile(imagePath);
result.imageFile = {
filename: path.basename(imagePath),
mimeType: 'image/jpeg',
@@ -86,7 +91,7 @@ app.get('/api/latest-snapshot', async (req, res) => {
} catch {}
try {
const jpg2 = await fs.readFile(imagePath2);
const jpg2 = await fsPromises.readFile(imagePath2);
result.image2 = {
filename: path.basename(imagePath2),
mimeType: 'image/jpeg',
@@ -160,6 +165,24 @@ async function checkServiceReachability(name, url) {
}
}
async function createHttpsServer() {
try {
await fsPromises.access(HTTPS_KEY_PATH);
await fsPromises.access(HTTPS_CERT_PATH);
const key = fs.readFileSync(HTTPS_KEY_PATH);
const cert = fs.readFileSync(HTTPS_CERT_PATH);
const httpsOptions = { key, cert, passphrase: HTTPS_PASSPHRASE };
console.log(`HTTPS-Zertifikate geladen: ${HTTPS_KEY_PATH}, ${HTTPS_CERT_PATH}`);
return https.createServer(httpsOptions, app);
} catch (err) {
console.warn('HTTPS-Zertifikate konnten nicht geladen werden:', err.message || err);
console.warn('Fallback auf HTTP. External proxy muss HTTPS terminieren.');
return null;
}
}
async function startServer() {
if (WEBCAM_URL) {
await checkServiceReachability('WEBCAM_URL', new URL('/health', WEBCAM_URL).toString());
@@ -169,8 +192,12 @@ async function startServer() {
await checkServiceReachability('BODYTRACKER_URL', new URL('/v1/health', BODYTRACKER_URL).toString());
}
app.listen(PORT, () => {
console.log(`appRobotHoming backend listening on port ${PORT}`);
const server = await createHttpsServer();
const isHttps = Boolean(server);
const listenServer = server || app;
listenServer.listen(PORT, () => {
console.log(`appRobotHoming backend listening on port ${PORT} (${isHttps ? 'HTTPS' : 'HTTP'})`);
console.log(`WEBCAM_URL=${WEBCAM_URL || '<lokal>'}`);
console.log(`BODYTRACKER_URL=${BODYTRACKER_URL || '<nicht konfiguriert>'}`);
});
@@ -180,7 +207,7 @@ startServer().catch((err) => {
console.error('Fehler beim Starten des Servers:', err);
console.log('Starte trotzdem den Server weiter...');
app.listen(PORT, () => {
console.log(`appRobotHoming backend listening on port ${PORT}`);
console.log(`appRobotHoming backend listening on port ${PORT} (HTTP)`);
console.log(`WEBCAM_URL=${WEBCAM_URL || '<lokal>'}`);
console.log(`BODYTRACKER_URL=${BODYTRACKER_URL || '<nicht konfiguriert>'}`);
});