import os from fastapi import FastAPI from fastapi.responses import HTMLResponse from app.api.routes import router from app.runtime import stats_exporter from app.utils.redis_client import RedisClient app = FastAPI(title="Smart KeyPool Gateway") app.include_router(router) @app.get("/", response_class=HTMLResponse) async def dashboard_home(): return await stats_exporter.render_dashboard() @app.get("/dashboard", response_class=HTMLResponse) async def dashboard_page(): return await stats_exporter.render_dashboard() @app.on_event("startup") async def startup_event(): redis = RedisClient.get_instance() try: await redis.ping() print("Connected to Redis") except Exception as e: print(f"Failed to connect to Redis: {e}") await stats_exporter.start() print("Stats exporter started - dashboard available at / or /dashboard") @app.on_event("shutdown") async def shutdown_event(): await stats_exporter.stop() await RedisClient.close() if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", "8888")))