from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager from core.config import settings from core.database import connect_to_mongo, close_mongo_connection from routers import auth, plants, scans, alerts, care, chat, ai # Lifespan context manager for startup/shutdown events @asynccontextmanager async def lifespan(app: FastAPI): # Startup await connect_to_mongo() yield # Shutdown await close_mongo_connection() # Create FastAPI app app = FastAPI( title="GreenBuddy API", description="Backend API for the GreenBuddy plant care application", version="1.0.0", lifespan=lifespan ) # Configure CORS app.add_middleware( CORSMiddleware, allow_origins=settings.allowed_origins_list, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include routers app.include_router(auth.router) app.include_router(plants.router) app.include_router(scans.router) app.include_router(alerts.router) app.include_router(care.router) app.include_router(chat.router) app.include_router(ai.router) # Health check endpoint @app.get("/health") async def health_check(): """Health check endpoint.""" return {"status": "ok"} # Root endpoint @app.get("/") async def root(): """Root endpoint with API information.""" return { "message": "GreenBuddy API", "version": "1.0.0", "docs": "/docs", "openapi": "/openapi.json" } if __name__ == "__main__": import uvicorn uvicorn.run( "main:app", host=settings.HOST, port=settings.PORT, reload=True )