| """FastAPI application for Tavily token management.""" |
| import os |
| import logging |
| from contextlib import asynccontextmanager |
|
|
| from fastapi import FastAPI |
| from fastapi.middleware.cors import CORSMiddleware |
| from fastapi.responses import FileResponse, HTMLResponse |
| from fastapi.staticfiles import StaticFiles |
|
|
| from . import db |
| from .routes import router, proxy_router |
|
|
| logging.basicConfig(level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
| STATIC_DIR = os.path.join(os.path.dirname(__file__), "static") |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| db.init_db() |
| yield |
|
|
|
|
| app = FastAPI(title="Search Key Manager", version="4.0.0", lifespan=lifespan) |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_methods=["GET", "POST", "DELETE", "PATCH", "OPTIONS"], |
| allow_headers=["Authorization", "Content-Type"], |
| ) |
|
|
| app.include_router(router) |
| app.include_router(proxy_router) |
|
|
|
|
| @app.get("/") |
| @app.get("/admin") |
| @app.get("/admin/{path:path}") |
| def index(): |
| index_path = os.path.join(STATIC_DIR, "index.html") |
| if os.path.exists(index_path): |
| return FileResponse(index_path) |
| return HTMLResponse("<h1>Tavily Key Manager</h1><p>Dashboard not found.</p>") |
|
|
|
|
| @app.get("/health") |
| def health(): |
| stats = db.get_stats() |
| return {"status": "ok", **stats} |
|
|
|
|
| if os.path.isdir(STATIC_DIR): |
| app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static") |
|
|