|
|
from fastapi import FastAPI |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
from contextlib import asynccontextmanager |
|
|
|
|
|
from api.routers import chats |
|
|
from api.routers import memory |
|
|
from db.session import create_db_and_tables |
|
|
from core.config import get_settings |
|
|
|
|
|
settings = get_settings() |
|
|
|
|
|
@asynccontextmanager |
|
|
async def lifespan(app: FastAPI): |
|
|
print(f"INFO: Starting up {settings.APP_NAME} v{settings.APP_VERSION}...") |
|
|
create_db_and_tables() |
|
|
print("INFO: Database tables checked/created.") |
|
|
yield |
|
|
print(f"INFO: Shutting down {settings.APP_NAME}...") |
|
|
|
|
|
|
|
|
app = FastAPI( |
|
|
title=settings.APP_NAME, |
|
|
description=f"An API for interacting with {settings.APP_NAME}.", |
|
|
version=settings.APP_VERSION, |
|
|
lifespan=lifespan |
|
|
) |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=[settings.FRONTEND_URL], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
app.include_router(chats.router, prefix="/api/chats", tags=["Chats & Messages"]) |
|
|
app.include_router(memory.router, prefix="/api/memories", tags=["Memory Management"]) |
|
|
|
|
|
|
|
|
@app.get("/", tags=["Health"]) |
|
|
async def read_root(): |
|
|
return {"status": "ok", "message": "Welcome to Makhfi AI!"} |