update
Browse files- frontend/server.js +21 -24
frontend/server.js
CHANGED
|
@@ -7,14 +7,14 @@ const { createProxyMiddleware } = require("http-proxy-middleware");
|
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
const port = process.env.PORT || 7860;
|
| 10 |
-
const apiPort = process.env.INTERNAL_API_PORT ||
|
| 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
|
| 18 |
: `http://127.0.0.1:${apiPort}`; // URL de développement
|
| 19 |
|
| 20 |
// Enable CORS for all routes
|
|
@@ -30,24 +30,7 @@ app.use((req, res, next) => {
|
|
| 30 |
next();
|
| 31 |
});
|
| 32 |
|
| 33 |
-
//
|
| 34 |
-
app.use(
|
| 35 |
-
express.static(path.join(__dirname, "build"), {
|
| 36 |
-
// Don't cache HTML files
|
| 37 |
-
setHeaders: (res, path) => {
|
| 38 |
-
if (path.endsWith(".html")) {
|
| 39 |
-
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
| 40 |
-
res.setHeader("Pragma", "no-cache");
|
| 41 |
-
res.setHeader("Expires", "0");
|
| 42 |
-
} else {
|
| 43 |
-
// Cache other static resources for 1 year
|
| 44 |
-
res.setHeader("Cache-Control", "public, max-age=31536000");
|
| 45 |
-
}
|
| 46 |
-
},
|
| 47 |
-
})
|
| 48 |
-
);
|
| 49 |
-
|
| 50 |
-
// Proxy only API routes to the Python backend
|
| 51 |
app.use(
|
| 52 |
[
|
| 53 |
"/health",
|
|
@@ -64,9 +47,6 @@ app.use(
|
|
| 64 |
createProxyMiddleware({
|
| 65 |
target: backendUrl,
|
| 66 |
changeOrigin: true,
|
| 67 |
-
pathRewrite: {
|
| 68 |
-
"^/api": "", // Remove /api prefix if present
|
| 69 |
-
},
|
| 70 |
onError: (err, req, res) => {
|
| 71 |
console.error("Proxy Error:", err);
|
| 72 |
res.status(500).json({ error: "Proxy Error", details: err.message });
|
|
@@ -74,7 +54,24 @@ app.use(
|
|
| 74 |
})
|
| 75 |
);
|
| 76 |
|
| 77 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
app.get("*", (req, res) => {
|
| 79 |
// Headers for client-side routing
|
| 80 |
res.set({
|
|
|
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
const port = process.env.PORT || 7860;
|
| 10 |
+
const apiPort = process.env.INTERNAL_API_PORT || 8000;
|
| 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.hf.space" // URL de production (same as frontend)
|
| 18 |
: `http://127.0.0.1:${apiPort}`; // URL de développement
|
| 19 |
|
| 20 |
// Enable CORS for all routes
|
|
|
|
| 30 |
next();
|
| 31 |
});
|
| 32 |
|
| 33 |
+
// Proxy API routes to the Python backend first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
app.use(
|
| 35 |
[
|
| 36 |
"/health",
|
|
|
|
| 47 |
createProxyMiddleware({
|
| 48 |
target: backendUrl,
|
| 49 |
changeOrigin: true,
|
|
|
|
|
|
|
|
|
|
| 50 |
onError: (err, req, res) => {
|
| 51 |
console.error("Proxy Error:", err);
|
| 52 |
res.status(500).json({ error: "Proxy Error", details: err.message });
|
|
|
|
| 54 |
})
|
| 55 |
);
|
| 56 |
|
| 57 |
+
// Then serve static files from the build directory
|
| 58 |
+
app.use(
|
| 59 |
+
express.static(path.join(__dirname, "build"), {
|
| 60 |
+
// Don't cache HTML files
|
| 61 |
+
setHeaders: (res, path) => {
|
| 62 |
+
if (path.endsWith(".html")) {
|
| 63 |
+
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
| 64 |
+
res.setHeader("Pragma", "no-cache");
|
| 65 |
+
res.setHeader("Expires", "0");
|
| 66 |
+
} else {
|
| 67 |
+
// Cache other static resources for 1 year
|
| 68 |
+
res.setHeader("Cache-Control", "public, max-age=31536000");
|
| 69 |
+
}
|
| 70 |
+
},
|
| 71 |
+
})
|
| 72 |
+
);
|
| 73 |
+
|
| 74 |
+
// Finally, handle all other routes by serving index.html
|
| 75 |
app.get("*", (req, res) => {
|
| 76 |
// Headers for client-side routing
|
| 77 |
res.set({
|