File size: 1,776 Bytes
5d1a8d2
 
 
 
 
 
cfcea40
5d1a8d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfcea40
 
5d1a8d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfcea40
5d1a8d2
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
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
    """
    # ── STARTUP ──
    logger.info("πŸš€ Initializing database...")
    init_db()
    logger.info("βœ… Database ready")
    
    # Start background backup scheduler
    backup_task = asyncio.create_task(scheduled_backup_task())
    logger.info("⏰ Backup scheduler started (every 30 min)")
    
    yield
    
    # ── SHUTDOWN ──
    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,
)

# ── Mount routes ──
app.include_router(admin_router)
app.include_router(api_router)

# ── Mount static files ──
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",
    }