|
| 1 | +const express = require('express'); |
| 2 | +const expressHttpProxy = require('express-http-proxy'); |
| 3 | +const cors = require('cors'); |
| 4 | + |
| 5 | +const PORT = 8080; |
| 6 | +const ZALLY_URL = process.env.ZALLY_URL || 'http://localhost:8000'; |
| 7 | + |
| 8 | +const app = express(); |
| 9 | + |
| 10 | +app.use( |
| 11 | + cors({ |
| 12 | + origin: (origin, callback) => { |
| 13 | + if (!origin) return callback(null, true); |
| 14 | + if (origin === 'http://localhost:3000') return callback(null, origin); |
| 15 | + return callback(new Error('Not allowed by CORS')); |
| 16 | + }, |
| 17 | + methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], |
| 18 | + allowedHeaders: [ |
| 19 | + 'Content-Type', |
| 20 | + 'Authorization', |
| 21 | + 'X-Requested-With', |
| 22 | + 'Accept', |
| 23 | + 'Origin', |
| 24 | + ], |
| 25 | + credentials: true, |
| 26 | + optionsSuccessStatus: 204, |
| 27 | + maxAge: 600, |
| 28 | + }), |
| 29 | +); |
| 30 | + |
| 31 | +// Basic health check |
| 32 | +app.get('/health', (_, res) => res.status(200).send('ok')); |
| 33 | +app.get('/api/auth/guest/refresh', (_, res) => res.sendStatus(200)); |
| 34 | +app.use( |
| 35 | + '/api/proxy/api-linter', |
| 36 | + // Short-circuit preflight for this route to avoid hitting the proxy |
| 37 | + (req, res, next) => { |
| 38 | + if (req.method === 'OPTIONS') { |
| 39 | + const origin = req.headers.origin || 'http://localhost:3000'; |
| 40 | + res.set({ |
| 41 | + 'Access-Control-Allow-Origin': origin, |
| 42 | + Vary: 'Origin', |
| 43 | + 'Access-Control-Allow-Credentials': 'true', |
| 44 | + 'Access-Control-Allow-Methods': 'GET,POST,PUT,PATCH,DELETE,OPTIONS', |
| 45 | + 'Access-Control-Allow-Headers': |
| 46 | + 'Content-Type, Authorization, X-Requested-With, Accept, Origin', |
| 47 | + 'Access-Control-Max-Age': '600', |
| 48 | + }); |
| 49 | + return res.sendStatus(204); |
| 50 | + } |
| 51 | + return next(); |
| 52 | + }, |
| 53 | + expressHttpProxy(ZALLY_URL, { |
| 54 | + https: false, |
| 55 | + proxyReqPathResolver: req => { |
| 56 | + return req.originalUrl.replace(/^\/api\/proxy\/api-linter/, ''); |
| 57 | + }, |
| 58 | + proxyErrorHandler: (err, res, next) => { |
| 59 | + if (err && (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND')) { |
| 60 | + // eslint-disable-next-line no-console |
| 61 | + console.error(err); |
| 62 | + res.status(502).json({ error: 'Bad gateway', detail: err.message }); |
| 63 | + } else { |
| 64 | + next(err); |
| 65 | + } |
| 66 | + }, |
| 67 | + parseReqBody: false, |
| 68 | + reqAsBuffer: true, |
| 69 | + userResHeaderDecorator: (headers, userReq) => { |
| 70 | + const origin = userReq.headers.origin; |
| 71 | + if (origin) { |
| 72 | + // Ensure no wildcard when credentials are used |
| 73 | + headers['access-control-allow-origin'] = origin; |
| 74 | + headers.vary = 'Origin'; |
| 75 | + headers['access-control-allow-credentials'] = 'true'; |
| 76 | + } |
| 77 | + if (headers['access-control-allow-origin'] === '*') { |
| 78 | + headers['access-control-allow-origin'] = |
| 79 | + origin || 'http://localhost:3000'; |
| 80 | + } |
| 81 | + return headers; |
| 82 | + }, |
| 83 | + userResDecorator: (proxyRes, proxyResData, userReq) => { |
| 84 | + // eslint-disable-next-line no-console |
| 85 | + console.log( |
| 86 | + `[proxy] ${userReq.method} ${userReq.originalUrl} -> ${proxyRes.statusCode}`, |
| 87 | + ); |
| 88 | + return proxyResData; |
| 89 | + }, |
| 90 | + }), |
| 91 | +); |
| 92 | + |
| 93 | +const server = app.listen(PORT, () => { |
| 94 | + // eslint-disable-next-line no-console |
| 95 | + console.log(`Backend proxy listening on http://localhost:${PORT}`); |
| 96 | +}); |
| 97 | + |
| 98 | +const shutdown = signal => { |
| 99 | + // eslint-disable-next-line no-console |
| 100 | + console.log(`\n${signal} received. Shutting down...`); |
| 101 | + server.close(() => process.exit(0)); |
| 102 | +}; |
| 103 | + |
| 104 | +['SIGINT', 'SIGTERM'].forEach(sig => process.on(sig, () => shutdown(sig))); |
0 commit comments