File size: 1,534 Bytes
a04b0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, HTMLResponse
import uvicorn

# Add server directory to path
sys.path.insert(0, 'server')

# Import the original app
from server.app import app as server_app

# Create the main app
app = FastAPI()

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount the original server app
app.mount("/api", server_app)

# Mount static directories
app.mount("/uploaded_images", StaticFiles(directory="server/uploaded_images"), name="uploaded_images")
app.mount("/static", StaticFiles(directory="server/static"), name="static")
app.mount("/assets", StaticFiles(directory="frontend/dist/assets"), name="assets")

# Serve frontend
@app.get("/{path:path}")
async def serve_frontend(path: str):
    # First check if the path exists in the frontend dist
    if os.path.exists(f"frontend/dist/{path}"):
        return FileResponse(f"frontend/dist/{path}")
    
    # Otherwise return the index.html
    return FileResponse("frontend/dist/index.html")

@app.get("/", response_class=HTMLResponse)
async def root():
    return FileResponse("frontend/dist/index.html")

if __name__ == "__main__":
    # Use port 7860 for Hugging Face Spaces
    uvicorn.run("app:app", host="0.0.0.0", port=7860)