ini / app_new.py
favoredone's picture
Upload 9 files
bd6de9f verified
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
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Import our backend API
from backend import app as backend_app
# Create main FastAPI application
app = FastAPI(title="Mining Dashboard")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount the backend API
app.mount("/api", backend_app)
# Mount static files
static_path = Path(__file__).parent / "static"
app.mount("/static", StaticFiles(directory=str(static_path)), name="static")
# Serve index.html for root path
@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))
# Error handler
@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__":
# Open the browser
webbrowser.open("http://localhost:8000")
# Start the server
uvicorn.run(app, host="0.0.0.0", port=8000)