| from dotenv import load_dotenv |
|
|
| load_dotenv() |
|
|
| from fastapi import FastAPI |
| from src.api.v1 import tasks, chat |
| from src.api.v1.auth import router as auth_router |
|
|
| |
| from fastapi.middleware.cors import CORSMiddleware |
| from src.core.database import create_db_and_tables |
| from typing import Dict, List, Optional |
| from datetime import datetime |
| from sqlmodel import Field, SQLModel, Relationship |
| from pydantic import BaseModel, Field as PydanticField |
| from contextlib import asynccontextmanager |
|
|
|
|
| class MessageResponse(BaseModel): |
| """Pydantic model for message responses.""" |
|
|
| id: int |
| conversation_id: int |
| content: str |
| sender: str |
| created_at: datetime |
| metadata: Optional[Dict] = None |
|
|
|
|
| class ConversationResponse(BaseModel): |
| """Pydantic model for conversation responses.""" |
|
|
| id: int |
| user_id: str |
| created_at: datetime |
| updated_at: datetime |
| title: Optional[str] = None |
| message_count: Optional[int] = None |
| last_message: Optional[str] = None |
|
|
|
|
| class ConversationCreate(BaseModel): |
| """Pydantic model for creating conversations.""" |
|
|
| title: Optional[str] = None |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| |
| create_db_and_tables() |
| yield |
| |
|
|
|
|
| app = FastAPI( |
| title="AI Chat Backend API", |
| version="1.0.0", |
| description="A RESTful API for AI-powered chat conversations with user authentication and conversation management.", |
| openapi_url="/openapi.json", |
| docs_url="/docs", |
| redoc_url="/redoc", |
| lifespan=lifespan, |
| ) |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=[ |
| "http://localhost:3000", |
| "https://ahmed-hackathon-ii-phase-iii.vercel.app", |
| ], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
|
|
| |
| app.include_router(tasks.router, prefix="/api/v1/tasks", tags=["tasks"]) |
| app.include_router(auth_router, prefix="/api/v1", tags=["auth"]) |
| app.include_router(chat.router, prefix="/api/v1/chat", tags=["chat"]) |
|
|
| |
| app.include_router(auth_router, tags=["auth-root"]) |
| app.include_router(tasks.router, prefix="/tasks", tags=["tasks-root"]) |
| app.include_router(chat.router, prefix="/chat", tags=["chat-root"]) |
|
|
|
|
| @app.get("/") |
| def read_root(): |
| return {"Hello": "World", "status": "running"} |
|
|
|
|
| @app.get("/health") |
| def health_check(): |
| return {"status": "healthy", "service": "AI Chat Backend API"} |
|
|
|
|
| if __name__ == "__main__": |
| import uvicorn |
| import os |
|
|
| port = int(os.getenv("PORT", 8000)) |
| uvicorn.run(app, host="0.0.0.0", port=port) |
|
|