| """ |
| Main FastAPI Application |
| ββββββββββββββββββββββββ |
| Ties everything together: API + Admin + MCP + Backup scheduler |
| |
| Sources: |
| - FastAPI: https://fastapi.tiangolo.com/ |
| """ |
|
|
| from fastapi import FastAPI |
| from fastapi.staticfiles import StaticFiles |
| from contextlib import asynccontextmanager |
| import asyncio |
| import logging |
|
|
| from app.database import init_db |
| from app.backup import scheduled_backup_task |
| from app.admin.router import router as admin_router |
| from app.api.routes import router as api_router |
|
|
| logging.basicConfig(level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| """ |
| Startup: init DB + start backup scheduler |
| Shutdown: cleanup |
| """ |
| |
| logger.info("π Initializing database...") |
| init_db() |
| logger.info("β
Database ready") |
| |
| |
| backup_task = asyncio.create_task(scheduled_backup_task()) |
| logger.info("β° Backup scheduler started (every 30 min)") |
| |
| yield |
| |
| |
| backup_task.cancel() |
| logger.info("π Shutting down") |
|
|
|
|
| app = FastAPI( |
| title="SQLite DBaaS - Admin Console", |
| description="SQLite + Litestream + HF Bucket powered database service", |
| version="1.0.0", |
| lifespan=lifespan, |
| ) |
|
|
| |
| app.include_router(admin_router) |
| app.include_router(api_router) |
|
|
| |
| app.mount("/static", StaticFiles(directory="static"), name="static") |
|
|
|
|
| @app.get("/") |
| async def root(): |
| return { |
| "status": "running", |
| "service": "SQLite DBaaS", |
| "admin": "/admin/login", |
| "docs": "/docs", |
| "health": "/admin/api/health", |
| } |
|
|