diff --git a/backend/index.js b/backend/index.js index 76450b1..ede24fc 100644 --- a/backend/index.js +++ b/backend/index.js @@ -15,35 +15,34 @@ const __dirname = path.dirname(__filename); const app = express(); app.use(express.json()); -// Statische Dateien ausliefern (Frontend Build) +// Statische Frontend-Dateien ausliefern app.use(express.static(path.join(__dirname, 'public'))); -// SPA Fallback für React-Router +// SPA-Fallback für React-Router app.get('*', (req, res, next) => { - if (req.path.startsWith('/api')) return next(); // API Requests weiterleiten + if (req.path.startsWith('/api')) return next(); res.sendFile(path.join(__dirname, 'public', 'index.html')); }); -const PORT = process.env.PORT || 5173; +// Backend-Port fix +const PORT = 4001; // HTTPS Agent für Self-Signed-Zertifikate const agent = new https.Agent({ rejectUnauthorized: false }); -// Axios-Instance für Portainer-Requests +// Axios-Instance für Portainer const axiosInstance = axios.create({ httpsAgent: agent, headers: { "X-API-Key": process.env.PORTAINER_API_KEY }, baseURL: process.env.PORTAINER_URL, }); -// In-Memory Store für Redeploy-Status -const redeployingStacks = {}; // { [stackId]: true/false } +// In-Memory Redeploy-Status +const redeployingStacks = {}; // HTTP Server + Socket.IO const server = http.createServer(app); -const io = new Server(server, { - cors: { origin: "*" } -}); +const io = new Server(server, { cors: { origin: "*" } }); io.on("connection", (socket) => { console.log("Client verbunden:", socket.id); @@ -52,40 +51,34 @@ io.on("connection", (socket) => { const broadcastRedeployStatus = (stackId, status) => { redeployingStacks[stackId] = status; io.emit("redeployStatus", { stackId, status }); - console.log(`Stack ${stackId} redeploying: ${status}`); }; // --- API Endpoints --- - app.get('/api/stacks', async (req, res) => { try { const stacksRes = await axiosInstance.get('/api/stacks'); - const stacksWithStatus = await Promise.all( stacksRes.data.map(async (stack) => { try { - const statusRes = await axiosInstance.get(`/api/stacks/${stack.Id}/images_status?refresh=true`); + const statusRes = await axiosInstance.get( + `/api/stacks/${stack.Id}/images_status?refresh=true` + ); let statusEmoji = statusRes.data.Status === 'outdated' ? '⚠️' : '✅'; return { ...stack, updateStatus: statusEmoji, redeploying: redeployingStacks[stack.Id] || false }; - } catch (err) { - console.error(`Fehler Remote Digest Stack ${stack.Id}:`, err.message); + } catch { return { ...stack, updateStatus: '❌', redeploying: redeployingStacks[stack.Id] || false }; } }) ); - stacksWithStatus.sort((a, b) => a.Name.localeCompare(b.Name)); res.json(stacksWithStatus); } catch (err) { - console.error('Fehler beim Abrufen der Stacks:', err.message); - if (err.response) res.status(err.response.status).json(err.response.data); - else res.status(500).json({ error: err.message }); + res.status(500).json({ error: err.message }); } }); app.put('/api/stacks/:id/redeploy', async (req, res) => { const { id } = req.params; - try { broadcastRedeployStatus(id, true); @@ -107,13 +100,10 @@ app.put('/api/stacks/:id/redeploy', async (req, res) => { await axiosInstance.post( `/api/endpoints/${stack.EndpointId}/docker/images/create?fromImage=${encodeURIComponent(imageName)}` ); - } catch (pullErr) { - console.error(`Fehler Pull ${imageName}:`, pullErr.message); - } + } catch {} } - await axiosInstance.put( - `/api/stacks/${id}`, + await axiosInstance.put(`/api/stacks/${id}`, { StackFileContent: stackFileContent, Prune: false, PullImage: true }, { params: { endpointId: stack.EndpointId } } ); @@ -123,9 +113,7 @@ app.put('/api/stacks/:id/redeploy', async (req, res) => { res.json({ success: true, message: 'Stack redeployed' }); } catch (err) { broadcastRedeployStatus(id, false); - console.error(`Fehler Redeploy Stack ${id}:`, err.message); - if (err.response) res.status(err.response.status).json(err.response.data); - else res.status(500).json({ error: err.message }); + res.status(500).json({ error: err.message }); } }); diff --git a/backend/index.js.bak b/backend/index.js.bak deleted file mode 100644 index 52d0094..0000000 --- a/backend/index.js.bak +++ /dev/null @@ -1,144 +0,0 @@ -import express from 'express'; -import dotenv from 'dotenv'; -import https from 'https'; -import axios from 'axios'; -import http from 'http'; -import { Server } from 'socket.io'; - -dotenv.config(); - -const app = express(); -app.use(express.json()); - -const PORT = 4000; - -// HTTPS Agent für Self-Signed-Zertifikate -const agent = new https.Agent({ rejectUnauthorized: false }); - -// Axios-Instance für alle Portainer-Requests -const axiosInstance = axios.create({ - httpsAgent: agent, - headers: { "X-API-Key": process.env.PORTAINER_API_KEY }, - baseURL: process.env.PORTAINER_URL, -}); - -// In-Memory Store für Redeploy-Status -const redeployingStacks = {}; // { [stackId]: true/false } - -// HTTP Server + Socket.IO -const server = http.createServer(app); -const io = new Server(server, { - cors: { origin: "*" } // ggf. auf Frontend-URL anpassen -}); - -// Socket.IO Verbindung -io.on("connection", (socket) => { - console.log("Client verbunden:", socket.id); -}); - -// Hilfsfunktion zum Broadcasten des Redeploy-Status -const broadcastRedeployStatus = (stackId, status) => { - redeployingStacks[stackId] = status; - io.emit("redeployStatus", { stackId, status }); - console.log(`Stack ${stackId} redeploying: ${status}`); -}; - -// Root-Endpoint -app.get('/', (req, res) => { - console.log("Root Endpoint aufgerufen"); - res.send('StackPulse Backend läuft. Nutze /api/stacks für die Daten.'); -}); - -// Alle Stacks abrufen -app.get('/api/stacks', async (req, res) => { - try { - const stacksRes = await axiosInstance.get('/api/stacks'); - - const stacksWithStatus = await Promise.all( - stacksRes.data.map(async (stack) => { - try { - const statusRes = await axiosInstance.get(`/api/stacks/${stack.Id}/images_status?refresh=true`); - let statusEmoji = '✅'; // up-to-date - if (statusRes.data.Status === 'outdated') statusEmoji = '⚠️'; // outdated - - return { - ...stack, - updateStatus: statusEmoji, - redeploying: redeployingStacks[stack.Id] || false, - }; - } catch (err) { - console.error(`Fehler beim Abrufen Remote Digest für Stack ${stack.Id}:`, err.message); - return { - ...stack, - updateStatus: '❌', // Fehler - redeploying: redeployingStacks[stack.Id] || false, - }; - } - }) - ); - - stacksWithStatus.sort((a, b) => a.Name.localeCompare(b.Name)); - res.json(stacksWithStatus); - } catch (err) { - console.error('Fehler beim Abrufen der Stacks:', err.message); - if (err.response) res.status(err.response.status).json(err.response.data); - else res.status(500).json({ error: err.message }); - } -}); - -// Redeploy eines Stacks -app.put('/api/stacks/:id/redeploy', async (req, res) => { - const { id } = req.params; - - try { - // Status auf "redeploying" setzen & an alle Clients senden - broadcastRedeployStatus(id, true); - - const stackRes = await axiosInstance.get(`/api/stacks/${id}`); - const stack = stackRes.data; - - if (stack.Type === 1) { - await axiosInstance.put(`/api/stacks/${id}/git/redeploy?endpointId=${stack.EndpointId}`); - } else if (stack.Type === 2) { - const fileRes = await axiosInstance.get(`/api/stacks/${id}/file`); - const stackFileContent = fileRes.data?.StackFileContent; - - if (!stackFileContent) throw new Error("Stack file konnte nicht geladen werden"); - - const services = fileRes.data?.Config?.services || {}; - for (const serviceName in services) { - const imageName = services[serviceName].image; - if (!imageName) continue; - try { - await axiosInstance.post( - `/api/endpoints/${stack.EndpointId}/docker/images/create?fromImage=${encodeURIComponent(imageName)}` - ); - } catch (pullErr) { - console.error(`Fehler beim Pull von ${imageName}:`, pullErr.message); - } - } - - await axiosInstance.put( - `/api/stacks/${id}`, - { StackFileContent: stackFileContent, Prune: false, PullImage: true }, - { params: { endpointId: stack.EndpointId } } - ); - } - - // Redeploy beendet, Status an alle Clients senden - broadcastRedeployStatus(id, false); - - res.json({ success: true, message: 'Stack redeployed' }); - } catch (err) { - // Fehler → Status zurücksetzen & an Clients senden - broadcastRedeployStatus(id, false); - console.error(`Fehler beim Redeploy von Stack ${id}:`, err.message); - if (err.response) res.status(err.response.status).json(err.response.data); - else res.status(500).json({ error: err.message }); - } -}); - -// Server starten -server.listen(PORT, '0.0.0.0', () => { - console.log(`Backend läuft auf Port ${PORT}`); -}); diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 67cdfd0..18ba8c0 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -8,15 +8,10 @@ export default defineConfig({ port: 5173, proxy: { "/api": { - target: "http://127.0.0.1:4000", // dein Backend + target: "http://localhost:4001", // dein Backend changeOrigin: true, }, }, - allowedHosts: [ - "10.10.10.23", // dein Dev-Rechner - "stackpulse.d-razz.de", // der Host, den du brauchst - "localhost", - ], + allowedHosts: "all", }, }); - diff --git a/scripts/start-dev.sh b/scripts/start-dev.sh index 4361402..8a59a51 100755 --- a/scripts/start-dev.sh +++ b/scripts/start-dev.sh @@ -25,7 +25,7 @@ cd .. echo "" echo "✅ StackPulse läuft lokal:" echo "Frontend (Vite Dev): http://localhost:5173" -echo "Backend API: http://localhost:4000" +echo "Backend API: http://localhost:4001" echo "Beenden mit STRG+C" # Prozesse überwachen