From 552223cc6d6ab2ae408b16ea355a2bdef2d1758e Mon Sep 17 00:00:00 2001 From: edwin Date: Tue, 3 Feb 2026 22:29:43 -0800 Subject: [PATCH] removed fastapi dep and added requirements --- app/backend/server.py | 45 ++++++++++++++++++++++++------------------- app/requirements.txt | 10 ++++++++++ 2 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 app/requirements.txt diff --git a/app/backend/server.py b/app/backend/server.py index 36e1780..7032d62 100644 --- a/app/backend/server.py +++ b/app/backend/server.py @@ -2,6 +2,7 @@ from fastapi import FastAPI, APIRouter from dotenv import load_dotenv from starlette.middleware.cors import CORSMiddleware from motor.motor_asyncio import AsyncIOMotorClient +from contextlib import asynccontextmanager import os import logging from pathlib import Path @@ -19,13 +20,6 @@ mongo_url = os.environ['MONGO_URL'] client = AsyncIOMotorClient(mongo_url) db = client[os.environ['DB_NAME']] -# Create the main app without a prefix -app = FastAPI() - -# Create a router with the /api prefix -api_router = APIRouter(prefix="/api") - - # Define Models class StatusCheck(BaseModel): model_config = ConfigDict(extra="ignore") # Ignore MongoDB's _id field @@ -37,6 +31,16 @@ class StatusCheck(BaseModel): class StatusCheckCreate(BaseModel): client_name: str +# Configure logging +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) +logger = logging.getLogger(__name__) + +# Create a router with the /api prefix +api_router = APIRouter(prefix="/api") + # Add your routes to the router instead of directly to app @api_router.get("/") async def root(): @@ -66,6 +70,19 @@ async def get_status_checks(): return status_checks +# Define lifespan event handler +@asynccontextmanager +async def lifespan(app: FastAPI): + # Startup code here + logger.info("Application startup") + yield + # Shutdown code here + logger.info("Application shutdown") + client.close() + +# Create the main app with lifespan handler +app = FastAPI(lifespan=lifespan) + # Include the router in the main app app.include_router(api_router) @@ -75,16 +92,4 @@ app.add_middleware( allow_origins=os.environ.get('CORS_ORIGINS', '*').split(','), allow_methods=["*"], allow_headers=["*"], -) - -# Configure logging -logging.basicConfig( - level=logging.INFO, - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' -) -logger = logging.getLogger(__name__) - -@app.on_event("shutdown") -async def shutdown_db_client(): - client.close() - +) \ No newline at end of file diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..2f5bc37 --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1,10 @@ +fastapi +dotenv +starlette +motor +#os +logging +pathlib +pydantic +typing +datetime \ No newline at end of file