from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse import os from dotenv import load_dotenv from app.routes import auth, users from app.db.connection import connect_to_mongo, close_mongo_connection load_dotenv() app = FastAPI( title="User Authentication Service", description="A simple CRUD app with user authentication using HTTP-only cookies", version="1.0.0" ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(auth.router, prefix="/api/auth", tags=["authentication"]) app.include_router(users.router, prefix="/api/users", tags=["users"]) @app.on_event("startup") async def startup_db_client(): await connect_to_mongo() @app.on_event("shutdown") async def shutdown_db_client(): await close_mongo_connection() @app.get("/") async def root(): return { "title": "User Authentication Service", "documentation": "/docs", "health": "/health" } @app.get("/health") async def health_check(): return {"status": "healthy", "service": "User Authentication Service"}