Skip to content

Commit

Permalink
[#38] upgrading to latest version of FastAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdash committed Feb 16, 2024
1 parent 43896a6 commit 798a1b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import os
from contextlib import asynccontextmanager

import uvicorn
from beanie import init_beanie
Expand All @@ -21,7 +22,13 @@
from api.exceptions import RepositoryException


app = FastAPI()
@asynccontextmanager
async def lifespan(app_: FastAPI):
await startup_db_client()
yield
await shutdown_db_client()

app = FastAPI(lifespan=lifespan)

app.add_middleware(
CORSMiddleware,
Expand All @@ -43,15 +50,13 @@ async def validation_exception_handler(request, exc: ValidationError):
status_code=status.HTTP_400_BAD_REQUEST)


@app.on_event("startup")
async def startup_db_client():
settings = get_settings()
app.mongodb_client = AsyncIOMotorClient(settings.db_connection_string)
app.mongodb = app.mongodb_client[settings.database_name]
await init_beanie(database=app.mongodb, document_models=[DatasetMetadataDOC, User, Submission])


@app.on_event("shutdown")
async def shutdown_db_client():
app.mongodb_client.close()

Expand Down Expand Up @@ -85,7 +90,8 @@ def handle_exit(self, sig: int, frame) -> None:
async def main():
"""Run FastAPI"""

server = Server(config=uvicorn.Config(app, workers=1, loop="asyncio", host="0.0.0.0", port=8000, forwarded_allow_ips="*"))
server = Server(config=uvicorn.Config(app, workers=1, loop="asyncio", host="0.0.0.0", port=8000,
forwarded_allow_ips="*"))
api = asyncio.create_task(server.serve())

await asyncio.wait([api])
Expand Down
2 changes: 1 addition & 1 deletion docker/requirements/api.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
requests
fastapi[all]==0.101.*
fastapi[all]==0.109.*
uvicorn[standard]
motor==3.*
beanie[httpx]==1.25.*
Expand Down

0 comments on commit 798a1b9

Please sign in to comment.