Scribbler310 commited on
Commit
f93287b
·
1 Parent(s): f32ce1f

Fix: Serve frontend from backend at root URL

Browse files
Files changed (2) hide show
  1. Dockerfile +14 -3
  2. backend/main.py +26 -0
Dockerfile CHANGED
@@ -1,18 +1,29 @@
1
- FROM python:3.12-slim
 
 
 
 
 
 
2
 
 
 
3
  WORKDIR /app
4
 
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y build-essential && rm -rf /var/lib/apt/lists/*
7
 
8
- # Copy and install dependencies
9
  COPY backend/requirements.txt ./backend/
10
  RUN pip install --no-cache-dir -r backend/requirements.txt
11
 
12
- # Copy everything needed
13
  COPY backend/ ./backend/
14
  COPY middleware/ ./middleware/
15
 
 
 
 
16
  # Hugging Face requires port 7860
17
  EXPOSE 7860
18
 
 
1
+ # Stage 1: Build Frontend
2
+ FROM node:20 AS frontend-build
3
+ WORKDIR /app/frontend
4
+ COPY frontend/package*.json ./
5
+ RUN npm install
6
+ COPY frontend/ ./
7
+ RUN npm run build
8
 
9
+ # Stage 2: Backend & Serve
10
+ FROM python:3.12-slim
11
  WORKDIR /app
12
 
13
  # Install system dependencies
14
  RUN apt-get update && apt-get install -y build-essential && rm -rf /var/lib/apt/lists/*
15
 
16
+ # Copy and install backend dependencies
17
  COPY backend/requirements.txt ./backend/
18
  RUN pip install --no-cache-dir -r backend/requirements.txt
19
 
20
+ # Copy backend and middleware code
21
  COPY backend/ ./backend/
22
  COPY middleware/ ./middleware/
23
 
24
+ # Copy the built frontend from Stage 1
25
+ COPY --from=frontend-build /app/frontend/dist ./frontend/dist
26
+
27
  # Hugging Face requires port 7860
28
  EXPOSE 7860
29
 
backend/main.py CHANGED
@@ -4,6 +4,9 @@ import pickle
4
  import sqlite3
5
  import pandas as pd
6
  from fastapi import FastAPI
 
 
 
7
  from fastapi.middleware.cors import CORSMiddleware
8
  from pydantic import BaseModel
9
  from dotenv import load_dotenv
@@ -255,3 +258,26 @@ def chat_with_bot(req: ChatRequest):
255
  print(f"Gemini API Error: {e}")
256
  return {"error": str(e)}
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import sqlite3
5
  import pandas as pd
6
  from fastapi import FastAPI
7
+ from fastapi.staticfiles import StaticFiles
8
+ from fastapi.responses import FileResponse
9
+
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from pydantic import BaseModel
12
  from dotenv import load_dotenv
 
258
  print(f"Gemini API Error: {e}")
259
  return {"error": str(e)}
260
 
261
+
262
+ # --- SERVE FRONTEND ---
263
+ FRONTEND_PATH = os.path.join(BASE_DIR, "..", "frontend", "dist")
264
+
265
+ if os.path.exists(FRONTEND_PATH):
266
+ # Mount the static files (CSS, JS, images)
267
+ app.mount("/static", StaticFiles(directory=FRONTEND_PATH), name="static")
268
+
269
+ # Catch-all route to serve the React index.html for any non-API route
270
+ @app.get("/{full_path:path}")
271
+ async def serve_frontend(full_path: str):
272
+ # If the path looks like an API call, let it 404 or be handled by other routes
273
+ if full_path.startswith("api/"):
274
+ return {"detail": "Not Found"}
275
+
276
+ index_file = os.path.join(FRONTEND_PATH, "index.html")
277
+ if os.path.exists(index_file):
278
+ return FileResponse(index_file)
279
+ return {"detail": "Frontend build not found"}
280
+ else:
281
+ @app.get("/")
282
+ def read_root():
283
+ return {"message": "Wafer Defect API is running. Frontend build folder not found."}