tfrere commited on
Commit
1e1eb3f
·
1 Parent(s): d6b6619
Files changed (1) hide show
  1. frontend/server.js +22 -22
frontend/server.js CHANGED
@@ -9,18 +9,35 @@ const app = express();
9
  const port = process.env.PORT || 7860;
10
  const apiPort = process.env.INTERNAL_API_PORT || 7861;
11
 
 
 
 
 
 
 
 
 
12
  // Enable CORS for all routes
13
  app.use(cors());
14
 
15
  // Enable GZIP compression
16
  app.use(compression());
17
 
18
- // Proxy all API requests to the Python backend
 
 
 
 
 
 
 
19
  app.use(
20
- "/api",
21
  createProxyMiddleware({
22
- target: `http://127.0.0.1:${apiPort}`,
23
  changeOrigin: true,
 
 
 
24
  onError: (err, req, res) => {
25
  console.error("Proxy Error:", err);
26
  res.status(500).json({ error: "Proxy Error", details: err.message });
@@ -45,25 +62,8 @@ app.use(
45
  })
46
  );
47
 
48
- // Middleware to preserve URL parameters
49
- app.use((req, res, next) => {
50
- // Don't interfere with API requests
51
- if (req.url.startsWith("/api")) {
52
- return next();
53
- }
54
-
55
- // Preserve original URL parameters
56
- req.originalUrl = req.url;
57
- next();
58
- });
59
-
60
  // Handle all other routes by serving index.html
61
  app.get("*", (req, res) => {
62
- // Don't interfere with API requests
63
- if (req.url.startsWith("/api")) {
64
- return next();
65
- }
66
-
67
  // Headers for client-side routing
68
  res.set({
69
  "Cache-Control": "no-cache, no-store, must-revalidate",
@@ -78,8 +78,8 @@ app.get("*", (req, res) => {
78
  app.listen(port, "0.0.0.0", () => {
79
  console.log(
80
  `Frontend server is running on port ${port} in ${
81
- process.env.NODE_ENV || "development"
82
  } mode`
83
  );
84
- console.log(`API proxy target: http://127.0.0.1:${apiPort}`);
85
  });
 
9
  const port = process.env.PORT || 7860;
10
  const apiPort = process.env.INTERNAL_API_PORT || 7861;
11
 
12
+ // Determine if we're in production (Hugging Face Spaces)
13
+ const isProduction = process.env.NODE_ENV === "production";
14
+
15
+ // Get the backend URL from environment or use localhost in development
16
+ const backendUrl = isProduction
17
+ ? "https://yourbench-yourbench-simple-demo-backend.hf.space" // URL de production
18
+ : `http://127.0.0.1:${apiPort}`; // URL de développement
19
+
20
  // Enable CORS for all routes
21
  app.use(cors());
22
 
23
  // Enable GZIP compression
24
  app.use(compression());
25
 
26
+ // Middleware to preserve URL parameters for frontend routes
27
+ app.use((req, res, next) => {
28
+ // Preserve original URL parameters
29
+ req.originalUrl = req.url;
30
+ next();
31
+ });
32
+
33
+ // Proxy all backend routes to the Python backend
34
  app.use(
 
35
  createProxyMiddleware({
36
+ target: backendUrl,
37
  changeOrigin: true,
38
+ pathRewrite: {
39
+ "^/api": "", // Remove /api prefix if present
40
+ },
41
  onError: (err, req, res) => {
42
  console.error("Proxy Error:", err);
43
  res.status(500).json({ error: "Proxy Error", details: err.message });
 
62
  })
63
  );
64
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  // Handle all other routes by serving index.html
66
  app.get("*", (req, res) => {
 
 
 
 
 
67
  // Headers for client-side routing
68
  res.set({
69
  "Cache-Control": "no-cache, no-store, must-revalidate",
 
78
  app.listen(port, "0.0.0.0", () => {
79
  console.log(
80
  `Frontend server is running on port ${port} in ${
81
+ isProduction ? "production" : "development"
82
  } mode`
83
  );
84
+ console.log(`API proxy target: ${backendUrl}`);
85
  });