Refactor app initialization to use async context manager for lifecycle management

This commit is contained in:
2026-05-31 21:45:19 +08:00
parent d17fb1c0de
commit 8ad465bebd
+12 -10
View File
@@ -2,8 +2,9 @@ from __future__ import annotations
import json
import os
from collections.abc import AsyncGenerator, Callable
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Callable
import httpx
from fastapi import FastAPI, Request
@@ -96,21 +97,22 @@ def create_app(config_path: str | None = None) -> FastAPI:
resolved_path = _resolve_config_path(config_path)
gateway_config = load_gateway_config(resolved_path)
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None]:
app.state.gateway_config = gateway_config
app.state.http_client = httpx.AsyncClient()
try:
yield
finally:
await app.state.http_client.aclose()
app = FastAPI(
title=gateway_config.settings.title,
version=gateway_config.settings.version,
description=gateway_config.settings.description,
lifespan=lifespan,
)
@app.on_event("startup")
async def startup() -> None:
app.state.http_client = httpx.AsyncClient()
app.state.gateway_config = gateway_config
@app.on_event("shutdown")
async def shutdown() -> None:
await app.state.http_client.aclose()
_register_management_routes(app)
_register_dynamic_routes(app, gateway_config)
return app