Spaces:
Running
Running
| const express = require('express'); | |
| const { createProxyMiddleware } = require('http-proxy-middleware'); | |
| const os = require('os'); | |
| const app = express(); | |
| const PORT = 7860; | |
| // --- CONFIGURATION --- | |
| // Minimum free RAM (in Megabytes) required to accept a new request | |
| const MIN_FREE_RAM_MB = 500; | |
| // How often to check the queue (in milliseconds) | |
| const QUEUE_CHECK_INTERVAL_MS = 2000; | |
| const requestQueue = []; | |
| // Get current free RAM in MB | |
| const getFreeRamMB = () => os.freemem() / (1024 * 1024); | |
| // π§ RAM MONITORING & QUEUEING MIDDLEWARE | |
| app.use((req, res, next) => { | |
| // We only want to queue Heavy requests (usually POST requests with files) | |
| // GET requests like status checks can bypass this if needed, but let's queue all for safety | |
| const freeRam = getFreeRamMB(); | |
| if (freeRam > MIN_FREE_RAM_MB) { | |
| next(); // Enough RAM, process immediately | |
| } else { | |
| console.log(`β οΈ [RAM LOW: ${freeRam.toFixed(2)} MB] Queuing request: ${req.method} ${req.url}`); | |
| requestQueue.push({ req, res, next, time: Date.now() }); | |
| } | |
| }); | |
| // π QUEUE PROCESSOR LOOP | |
| setInterval(() => { | |
| if (requestQueue.length > 0) { | |
| const freeRam = getFreeRamMB(); | |
| if (freeRam > MIN_FREE_RAM_MB) { | |
| // Remove the first request from the queue | |
| const { req, res, next, time } = requestQueue.shift(); | |
| const waitTime = ((Date.now() - time) / 1000).toFixed(1); | |
| console.log(`β [RAM OK: ${freeRam.toFixed(2)} MB] Processing queued request: ${req.method} ${req.url} (Waited ${waitTime}s)`); | |
| next(); // Continue processing | |
| } | |
| } | |
| }, QUEUE_CHECK_INTERVAL_MS); | |
| // π REVERSE PROXY ROUTES | |
| const proxyOptions = { | |
| changeOrigin: true, | |
| proxyTimeout: 300000, // 5 minutes timeout for heavy tasks | |
| }; | |
| app.use('/visuals', createProxyMiddleware({ target: 'http://127.0.0.1:7861', pathRewrite: {'^/visuals': ''}, ...proxyOptions })); | |
| app.use('/audio_server', createProxyMiddleware({ target: 'http://127.0.0.1:7862', pathRewrite: {'^/audio_server': ''}, ...proxyOptions })); | |
| app.use('/cipher_sticker', createProxyMiddleware({ target: 'http://127.0.0.1:7863', pathRewrite: {'^/cipher_sticker': ''}, ...proxyOptions })); | |
| app.use('/scanner', createProxyMiddleware({ target: 'http://127.0.0.1:7864', pathRewrite: {'^/scanner': ''}, ...proxyOptions })); | |
| // Status Route | |
| app.get('/', (req, res) => { | |
| res.json({ | |
| message: 'π Cipher-MD Smart API Gateway is Running!', | |
| status: 'Active', | |
| ram_monitoring: true, | |
| free_ram_mb: Math.round(getFreeRamMB()), | |
| queued_requests: requestQueue.length | |
| }); | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`\n======================================================`); | |
| console.log(`π SMART API GATEWAY RUNNING ON PORT ${PORT}`); | |
| console.log(`π§ RAM THRESHOLD: ${MIN_FREE_RAM_MB} MB`); | |
| console.log(`======================================================\n`); | |
| }); | |