Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Replace deprecated FastAPI.on_event with lifespan event
Browse files Browse the repository at this point in the history
  • Loading branch information
cauebs committed Nov 14, 2023
1 parent b640371 commit 5dafa5b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/jenkins_to_github_notify/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
delegating to other modules the actual work.
"""
import logging
from contextlib import asynccontextmanager
from typing import AsyncIterator

from dotenv import dotenv_values
from fastapi import FastAPI
Expand All @@ -14,13 +16,12 @@
from jenkins_to_github_notify.notify import validate_event
from jenkins_to_github_notify.notify import validate_secret

app = FastAPI()
config: dict[str, str] = {}
logger = logging.getLogger("app")
config: dict[str, str] = {}


@app.on_event("startup")
async def startup_event() -> None:
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
"""Fail early during startup in case any variable is missing."""
for key, value in dotenv_values(".env").items():
assert isinstance(value, str), f"Unexpected type in config value: {value!r} {type(value)}"
Expand All @@ -29,6 +30,10 @@ async def startup_event() -> None:
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)-8s")
logger.setLevel(logging.INFO)
logger.info("Configuration validated.")
yield


app = FastAPI(lifespan=lifespan)


@app.get("/jobs/notify")
Expand Down

0 comments on commit 5dafa5b

Please sign in to comment.