File size: 2,006 Bytes
c9abf3f
 
 
 
 
 
 
a8e2a77
331e3ee
b10bcbc
c9abf3f
b10bcbc
c9abf3f
 
 
 
 
 
 
 
 
 
331e3ee
a8e2a77
 
 
331e3ee
 
 
 
 
 
 
 
 
 
a8e2a77
c9abf3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from datetime import datetime
from app.config import settings
from app.database import engine, Base
from app.api.v1.api import api_router
import chromadb
from chromadb.api.models.Collection import Collection
from dotenv import load_dotenv

load_dotenv()

@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan manager"""
    print("🏗️ Server starting:", datetime.now())
    print("🔧 Creating tables if they don't exist...")
    
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    
    client = await chromadb.AsyncHttpClient(
        host=settings.chroma_host,
        port=settings.chroma_port
    )
    app.state.chroma_client = client

    try:
        collection: Collection = await client.get_or_create_collection(settings.chroma_collection)
        app.state.chroma_collection = collection

        count = await collection.count()
        print(f"Successfully loaded collection '{settings.chroma_collection}' with {count} documents.")
    except Exception as e:
        print(f"Failed to load ChromaDB collection: {e}")

    print("✅ Tables ready!")
    yield
    print("🧹 Server shutting down:", datetime.now())


# Create FastAPI application
app = FastAPI(
    title=settings.APP_NAME,
    description=settings.APP_DESCRIPTION,
    version=settings.APP_VERSION,
    lifespan=lifespan
)

# CORS Configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.CORS_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include API router
app.include_router(api_router, prefix="/api/v1")


# Health check endpoint
@app.get("/", tags=["Health"])
async def root():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "message": f"{settings.APP_NAME} is running",
        "version": settings.APP_VERSION
    }