1818from fastapi import FastAPI , HTTPException , Response
1919from fastapi .responses import FileResponse , StreamingResponse
2020from fastapi .middleware .cors import CORSMiddleware
21+ from fastapi .staticfiles import StaticFiles
2122from pydantic import BaseModel
2223import io
2324import json
2930from typing import Optional
3031import urllib .parse
3132import requests
32-
3333# ============ PATH SETUP ============
3434BASE_OUTPUT = sys .argv [1 ] if len (sys .argv ) > 1 else os .environ .get ('UNS_DATA_DIR' , os .path .join (os .getcwd (), "output" ))
3535
@@ -458,6 +458,22 @@ def get_local_ip():
458458def health_check ():
459459 return {"status" : "ok" , "version" : "1.0.0" }
460460
461+ # ============ SERVE REACT FRONTEND TO MOBILE DEVICES ============
462+ # Determine the correct path to the frontend files based on environment
463+ if getattr (sys , 'frozen' , False ):
464+ # Packaged Production (PyInstaller): frontend is adjacent to the backend executable
465+ backend_dir = os .path .dirname (sys .executable )
466+ frontend_dir = os .path .abspath (os .path .join (backend_dir , ".." , "frontend" ))
467+ else :
468+ # Local Development
469+ frontend_dir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), ".." , "frontend" , "dist" ))
470+
471+ if os .path .exists (frontend_dir ):
472+ # CRITICAL: This must be the very last route added to the app!
473+ app .mount ("/" , StaticFiles (directory = frontend_dir , html = True ), name = "frontend" )
474+ else :
475+ print (f"Warning: Frontend directory not found at { frontend_dir } . Mobile web view will not work." )
476+
461477if __name__ == "__main__" :
462478 import uvicorn
463479 uvicorn .run (app , host = "0.0.0.0" , port = 8000 )
0 commit comments