|
|
from fastapi import FastAPI, HTTPException
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
from fastapi.responses import FileResponse, JSONResponse
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
import uvicorn
|
|
|
import webbrowser
|
|
|
from pathlib import Path
|
|
|
import os
|
|
|
import logging
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
from backend import app as backend_app
|
|
|
|
|
|
|
|
|
app = FastAPI(title="Mining Dashboard")
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
CORSMiddleware,
|
|
|
allow_origins=["*"],
|
|
|
allow_credentials=True,
|
|
|
allow_methods=["*"],
|
|
|
allow_headers=["*"],
|
|
|
)
|
|
|
|
|
|
|
|
|
app.mount("/api", backend_app)
|
|
|
|
|
|
|
|
|
static_path = Path(__file__).parent / "static"
|
|
|
app.mount("/static", StaticFiles(directory=str(static_path)), name="static")
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
async def serve_index():
|
|
|
try:
|
|
|
index_path = static_path / "index.html"
|
|
|
if not index_path.exists():
|
|
|
logger.error(f"Index file not found at {index_path}")
|
|
|
raise HTTPException(status_code=404, detail="Index file not found")
|
|
|
return FileResponse(str(index_path))
|
|
|
except Exception as e:
|
|
|
logger.error(f"Error serving index: {str(e)}")
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
@app.exception_handler(HTTPException)
|
|
|
async def http_exception_handler(request, exc):
|
|
|
return JSONResponse(
|
|
|
status_code=exc.status_code,
|
|
|
content={"detail": exc.detail}
|
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
webbrowser.open("http://localhost:8000")
|
|
|
|
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000) |