Files
mtusi2026lab3/app/main.py
T
2026-10-30 00:53:49 +03:00

81 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
import uvicorn
from fastapi import Depends, FastAPI
from app.core.config import Settings
from app.core.database import create_db_engine, create_session_factory
from app.core.redis import create_redis_client
from app.core.request_capacity import acquire_request_slot, create_request_capacity
from app.core.request_id import RequestIdMiddleware
from app.endpoints.auth import router as auth_router
from app.endpoints.chats import router as chats_router
from app.endpoints.health import router as health_router
from app.endpoints.messages import router as messages_router
from app.endpoints.users import router as users_router
from app.errors.handlers import register_exception_handlers
def configure_logging(settings: Settings) -> None:
logging.basicConfig(
level=settings.log_level,
format="%(asctime)s %(levelname)s %(name)s %(message)s",
)
def create_app(settings: Settings | None = None) -> FastAPI:
settings = settings or Settings() # type: ignore
configure_logging(settings)
engine = create_db_engine(settings)
redis = create_redis_client(settings)
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncGenerator[None]:
yield
await redis.aclose()
await engine.dispose()
application = FastAPI(title="Messenger", version="1.3.0", lifespan=lifespan)
application.state.settings = settings
application.state.engine = engine
application.state.session_factory = create_session_factory(engine)
application.state.redis = redis
application.state.request_capacity = create_request_capacity(settings.max_in_flight)
# TODO(3.2): application.state.instance_id = resolve_instance_id(settings.instance_id)
# и application.add_middleware(InstanceIdMiddleware, instance_id=...)
# (app/core/instance.py). Middleware, добавленный последним, — внешний.
# TODO(4.1): application.state.lifecycle = Lifecycle() (app/core/lifecycle.py)
application.add_middleware(RequestIdMiddleware)
register_exception_handlers(application)
limited = [Depends(acquire_request_slot)]
application.include_router(health_router)
application.include_router(users_router, dependencies=limited)
application.include_router(auth_router, dependencies=limited)
application.include_router(chats_router, dependencies=limited)
application.include_router(messages_router, dependencies=limited)
return application
def main() -> None:
settings = Settings() # type: ignore
uvicorn.run(
"app.main:create_app",
factory=True,
host=settings.app_host,
port=settings.app_port,
log_level=settings.log_level.lower(),
# TODO(4.1): согласовать с stop_grace_period в compose.yaml и drain-периодом
timeout_graceful_shutdown=settings.graceful_shutdown_timeout_seconds,
)
if __name__ == "__main__":
main()