File size: 2,255 Bytes
e7abd9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require("express");
const cors = require("cors");
const compression = require("compression");
const path = require("path");
const serveStatic = require("serve-static");
const { createProxyMiddleware } = require("http-proxy-middleware");

const app = express();
const port = process.env.PORT || 7860;
const apiPort = process.env.INTERNAL_API_PORT || 7861;

// Enable CORS for all routes
app.use(cors());

// Enable GZIP compression
app.use(compression());

// Proxy all API requests to the Python backend
app.use(
  "/api",
  createProxyMiddleware({
    target: `http://127.0.0.1:${apiPort}`,
    changeOrigin: true,
    onError: (err, req, res) => {
      console.error("Proxy Error:", err);
      res.status(500).json({ error: "Proxy Error", details: err.message });
    },
  })
);

// Serve static files from the build directory
app.use(
  express.static(path.join(__dirname, "build"), {
    // Don't cache HTML files
    setHeaders: (res, path) => {
      if (path.endsWith(".html")) {
        res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        res.setHeader("Pragma", "no-cache");
        res.setHeader("Expires", "0");
      } else {
        // Cache other static resources for 1 year
        res.setHeader("Cache-Control", "public, max-age=31536000");
      }
    },
  })
);

// Middleware to preserve URL parameters
app.use((req, res, next) => {
  // Don't interfere with API requests
  if (req.url.startsWith("/api")) {
    return next();
  }

  // Preserve original URL parameters
  req.originalUrl = req.url;
  next();
});

// Handle all other routes by serving index.html
app.get("*", (req, res) => {
  // Don't interfere with API requests
  if (req.url.startsWith("/api")) {
    return next();
  }

  // Headers for client-side routing
  res.set({
    "Cache-Control": "no-cache, no-store, must-revalidate",
    Pragma: "no-cache",
    Expires: "0",
  });

  // Send index.html for all other routes
  res.sendFile(path.join(__dirname, "build", "index.html"));
});

app.listen(port, "0.0.0.0", () => {
  console.log(
    `Frontend server is running on port ${port} in ${
      process.env.NODE_ENV || "development"
    } mode`
  );
  console.log(`API proxy target: http://127.0.0.1:${apiPort}`);
});