Skip to content

Commit

Permalink
Replace deprecated startup and shutdown events with lifespan
Browse files Browse the repository at this point in the history
  • Loading branch information
spyker77 committed Oct 31, 2023
1 parent 59f2ce1 commit 5a245f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

This is the implementation of [the course](https://testdriven.io/courses/tdd-fastapi/) with the following changes so far:

- Python image updated to the latest version 3.11
- Dependencies updated to the latest version at the moment
- CORSMiddleware used to manually control allowed origins
- Venv replaced with PDM
Expand All @@ -16,7 +15,7 @@ This is the implementation of [the course](https://testdriven.io/courses/tdd-fas
- Migrated to the Container registry from the Docker registry
- Implemented authentication and authorization using OAuth2
- Tortoise-ORM has been replaced by SQLAlchemy
- Use the transformer model instead of NLP from Newspaper3k
- Transformers model is used instead of NLP from Newspaper3k

## Quick Start

Expand All @@ -34,6 +33,12 @@ docker compose exec web alembic upgrade head

Open in your browser: <http://localhost:8000/docs>

Tests can be run with this command:

```bash
docker compose exec web pytest -n auto --cov
```

## Note

Current implementation of text summarization using the transformer model is not ideal for production:
Expand Down
19 changes: 10 additions & 9 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import List
from contextlib import asynccontextmanager
from typing import AsyncContextManager, List

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -16,13 +17,14 @@
API_VERSIONS_ROUTERS = {"v2": api_router_v2}


def create_application(api_versions: List[str] = ["v2"]) -> FastAPI:
def create_application(api_versions: List[str] = ["v2"], lifespan: AsyncContextManager = None) -> FastAPI:
application = FastAPI(
title="Test-Driven Development with FastAPI and Docker",
description="""Create a random user and then authorize to play around with options.
Authorized users can create and read everything, but update and delete only their own entries.
""",
version="v2",
lifespan=lifespan,
)

application.add_middleware(
Expand All @@ -42,17 +44,16 @@ def create_application(api_versions: List[str] = ["v2"]) -> FastAPI:
return application


app = create_application()


@app.on_event("startup")
async def startup_event():
@asynccontextmanager
async def lifespan(app: FastAPI):
log.info("Starting up...")
async with async_engine.begin():
pass

yield

@app.on_event("shutdown")
async def shutdown_event():
log.info("Shutting down...")
await async_engine.dispose()


app = create_application(lifespan=lifespan)
14 changes: 6 additions & 8 deletions app/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import logging

import pytest
from fastapi import FastAPI

from app.main import shutdown_event, startup_event
from app.main import lifespan


@pytest.mark.asyncio
async def test_startup_event_logging(caplog):
async def test_lifespan_logging(caplog):
caplog.set_level(logging.INFO)
await startup_event()
assert "Starting up..." in caplog.text
app = FastAPI()

async with lifespan(app):
assert "Starting up..." in caplog.text

@pytest.mark.asyncio
async def test_shutdown_event_logging(caplog):
caplog.set_level(logging.INFO)
await shutdown_event()
assert "Shutting down..." in caplog.text

0 comments on commit 5a245f9

Please sign in to comment.