proxy.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * GitNexus reverse proxy — serves production web UI + proxies /api/* to backend.
  3. * Zero dependencies, Node.js built-ins only.
  4. *
  5. * Usage: node proxy.mjs <dist-dir> [port]
  6. * dist-dir: path to gitnexus-web/dist (production build)
  7. * port: listen port (default: 8888)
  8. *
  9. * Environment:
  10. * API_PORT: GitNexus serve backend port (default: 4747)
  11. */
  12. import http from 'node:http';
  13. import fs from 'node:fs';
  14. import path from 'node:path';
  15. const API_PORT = parseInt(process.env.API_PORT || '4747');
  16. const DIST_DIR = process.argv[2] || './dist';
  17. const PORT = parseInt(process.argv[3] || '8888');
  18. const MIME = {
  19. '.html': 'text/html',
  20. '.js': 'application/javascript',
  21. '.css': 'text/css',
  22. '.json': 'application/json',
  23. '.png': 'image/png',
  24. '.svg': 'image/svg+xml',
  25. '.ico': 'image/x-icon',
  26. '.woff2': 'font/woff2',
  27. '.woff': 'font/woff',
  28. '.wasm': 'application/wasm',
  29. '.ttf': 'font/ttf',
  30. '.map': 'application/json',
  31. };
  32. function proxyToApi(req, res) {
  33. const opts = {
  34. hostname: '127.0.0.1',
  35. port: API_PORT,
  36. path: req.url,
  37. method: req.method,
  38. headers: { ...req.headers, host: `127.0.0.1:${API_PORT}` },
  39. };
  40. const proxy = http.request(opts, (upstream) => {
  41. res.writeHead(upstream.statusCode, upstream.headers);
  42. upstream.pipe(res, { end: true });
  43. });
  44. proxy.on('error', () => {
  45. res.writeHead(502, { 'Content-Type': 'text/plain' });
  46. res.end('GitNexus backend unavailable — is `npx gitnexus serve` running?');
  47. });
  48. req.pipe(proxy, { end: true });
  49. }
  50. function serveStatic(req, res) {
  51. const urlPath = req.url.split('?')[0];
  52. let filePath = path.join(DIST_DIR, urlPath === '/' ? 'index.html' : urlPath);
  53. // SPA fallback: if file doesn't exist and isn't a static asset, serve index.html
  54. if (!fs.existsSync(filePath) && !path.extname(filePath)) {
  55. filePath = path.join(DIST_DIR, 'index.html');
  56. }
  57. const ext = path.extname(filePath);
  58. const mime = MIME[ext] || 'application/octet-stream';
  59. try {
  60. const data = fs.readFileSync(filePath);
  61. res.writeHead(200, {
  62. 'Content-Type': mime,
  63. 'Cache-Control': ext === '.html' ? 'no-cache' : 'public, max-age=86400',
  64. });
  65. res.end(data);
  66. } catch {
  67. res.writeHead(404, { 'Content-Type': 'text/plain' });
  68. res.end('Not found');
  69. }
  70. }
  71. const server = http.createServer((req, res) => {
  72. if (req.url.startsWith('/api')) {
  73. proxyToApi(req, res);
  74. } else {
  75. serveStatic(req, res);
  76. }
  77. });
  78. server.listen(PORT, () => {
  79. console.log(`GitNexus proxy listening on http://localhost:${PORT}`);
  80. console.log(` Web UI: http://localhost:${PORT}/`);
  81. console.log(` API: http://localhost:${PORT}/api/repos`);
  82. console.log(` Backend: http://127.0.0.1:${API_PORT}`);
  83. });