File size: 1,526 Bytes
30c2a23
d41e7ae
 
 
 
 
 
30c2a23
d41e7ae
30c2a23
 
d41e7ae
 
 
30c2a23
d41e7ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30c2a23
d41e7ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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"]