test: add local proxy for wizard/backend/frontend
This commit is contained in:
@@ -28,4 +28,4 @@ WORKDIR /app
|
|||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install && npm install -g playwright && playwright install chromium
|
RUN npm install && npm install -g playwright && playwright install chromium
|
||||||
COPY . .
|
COPY . .
|
||||||
CMD ["bash", "-lc", "node server/wizard-proxy.mjs & npx playwright test"]
|
CMD ["bash", "-lc", "node server/proxy.mjs & npx playwright test"]
|
||||||
|
|||||||
55
server/proxy.mjs
Normal file
55
server/proxy.mjs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import http from 'http'
|
||||||
|
|
||||||
|
const createProxy = ({ name, listenPort, targetHost, targetPort }) => {
|
||||||
|
http
|
||||||
|
.createServer((req, res) => {
|
||||||
|
const proxyReq = http.request(
|
||||||
|
{
|
||||||
|
hostname: targetHost,
|
||||||
|
port: targetPort,
|
||||||
|
path: req.url,
|
||||||
|
method: req.method,
|
||||||
|
headers: req.headers,
|
||||||
|
},
|
||||||
|
(proxyRes) => {
|
||||||
|
res.writeHead(proxyRes.statusCode || 502, proxyRes.headers)
|
||||||
|
proxyRes.pipe(res)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
proxyReq.on('error', (err) => {
|
||||||
|
res.statusCode = 502
|
||||||
|
res.end(`Bad gateway: ${err.message}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
req.pipe(proxyReq)
|
||||||
|
})
|
||||||
|
.listen(listenPort, '127.0.0.1', () => {
|
||||||
|
console.log(`${name} proxy listening on 127.0.0.1:${listenPort} -> ${targetHost}:${targetPort}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const wizardPort = Number(process.env.WIZARD_PORT || 8080)
|
||||||
|
const backendPort = Number(process.env.BACKEND_PORT || 8000)
|
||||||
|
const frontendPort = Number(process.env.FRONTEND_PORT || 3000)
|
||||||
|
|
||||||
|
createProxy({
|
||||||
|
name: 'wizard',
|
||||||
|
listenPort: wizardPort,
|
||||||
|
targetHost: process.env.WIZARD_HOST || 'wizard',
|
||||||
|
targetPort: wizardPort,
|
||||||
|
})
|
||||||
|
|
||||||
|
createProxy({
|
||||||
|
name: 'backend',
|
||||||
|
listenPort: backendPort,
|
||||||
|
targetHost: process.env.BACKEND_HOST || 'backend',
|
||||||
|
targetPort: backendPort,
|
||||||
|
})
|
||||||
|
|
||||||
|
createProxy({
|
||||||
|
name: 'frontend',
|
||||||
|
listenPort: frontendPort,
|
||||||
|
targetHost: process.env.FRONTEND_HOST || 'frontend',
|
||||||
|
targetPort: frontendPort,
|
||||||
|
})
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import http from 'http'
|
|
||||||
|
|
||||||
const listenPort = Number(process.env.WIZARD_PORT || 8080)
|
|
||||||
const targetHost = process.env.WIZARD_HOST || 'wizard'
|
|
||||||
const targetPort = Number(process.env.WIZARD_PORT || 8080)
|
|
||||||
|
|
||||||
http
|
|
||||||
.createServer((req, res) => {
|
|
||||||
const proxyReq = http.request(
|
|
||||||
{
|
|
||||||
hostname: targetHost,
|
|
||||||
port: targetPort,
|
|
||||||
path: req.url,
|
|
||||||
method: req.method,
|
|
||||||
headers: req.headers,
|
|
||||||
},
|
|
||||||
(proxyRes) => {
|
|
||||||
res.writeHead(proxyRes.statusCode || 502, proxyRes.headers)
|
|
||||||
proxyRes.pipe(res)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
proxyReq.on('error', (err) => {
|
|
||||||
res.statusCode = 502
|
|
||||||
res.end(`Bad gateway: ${err.message}`)
|
|
||||||
})
|
|
||||||
|
|
||||||
req.pipe(proxyReq)
|
|
||||||
})
|
|
||||||
.listen(listenPort, '127.0.0.1', () => {
|
|
||||||
console.log(`wizard proxy listening on 127.0.0.1:${listenPort} -> ${targetHost}:${targetPort}`)
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user