Spaces:
Running
Running
| FROM python:3.10-slim | |
| RUN apt-get update && apt-get install -y curl ca-certificates gnupg && \ | |
| curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ | |
| apt-get install -y nodejs && \ | |
| rm -rf /var/lib/apt/lists/* | |
| WORKDIR /app | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| RUN npm install http-proxy | |
| COPY . . | |
| RUN cat > /app/gateway.js << 'GWEOF' | |
| var http = require('http'); | |
| var httpProxy = require('http-proxy'); | |
| var DOMAIN = process.env.DOMAIN || ''; | |
| var proxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:3001', ws: true }); | |
| proxy.on('error', function(e, req, res) { | |
| if (res && res.writeHead) { res.writeHead(502); res.end('502'); } | |
| }); | |
| function ok(req) { | |
| if (!DOMAIN) return true; | |
| var h = (req.headers['x-real-domain'] || '') + '|' + (req.headers['x-forwarded-host'] || '') + '|' + (req.headers['host'] || ''); | |
| return h.indexOf(DOMAIN) !== -1; | |
| } | |
| var s = http.createServer(function(req, res) { | |
| if (ok(req)) { | |
| proxy.web(req, res); | |
| } else { | |
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| res.end('200 OK'); | |
| } | |
| }); | |
| s.on('upgrade', function(req, socket, head) { | |
| if (ok(req)) { proxy.ws(req, socket, head); } else { socket.destroy(); } | |
| }); | |
| s.listen(7860, function() { console.log('[GW] 7860 -> 3001'); }); | |
| GWEOF | |
| RUN cat > /start.sh << 'SHEOF' | |
| #!/bin/sh | |
| python -m uvicorn main:app --host 0.0.0.0 --port 3001 & | |
| sleep 3 | |
| exec node /app/gateway.js | |
| SHEOF | |
| RUN chmod +x /start.sh | |
| EXPOSE 7860 | |
| CMD ["/start.sh"] |