forked from mtusi2026/mtusi2026lab2
feat: функционал первой лабы перенос
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
||||
from application import api_endpoints, health_endpoints
|
||||
from application.api.handlers import register_exception_handlers
|
||||
from application.api.middlewares import RequestContextMiddleware
|
||||
from config import APP_ENV, APP_HOST, APP_PORT, LOGGER, SHUTDOWN_TIMEOUT_SEC
|
||||
from infrastructure import InMemoryStorage
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||
app.state.storage = InMemoryStorage()
|
||||
app.state.is_ready = True
|
||||
LOGGER.info(f"Application started, env={APP_ENV}, state is in-memory only")
|
||||
yield
|
||||
app.state.is_ready = False
|
||||
LOGGER.info("Application stopped, in-memory state discarded")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="mtuci-labs-messanger",
|
||||
description="мессенджер-конкурент-максу",
|
||||
version="1.2.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
app.add_middleware(RequestContextMiddleware)
|
||||
register_exception_handlers(app)
|
||||
|
||||
app.include_router(health_endpoints)
|
||||
app.include_router(api_endpoints)
|
||||
|
||||
|
||||
def main(host: str, port: int, shutdown_timeout: int) -> None:
|
||||
"""Функция для запуска api"""
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
timeout_graceful_shutdown=shutdown_timeout,
|
||||
access_log=False,
|
||||
server_header=False,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(host=APP_HOST, port=APP_PORT, shutdown_timeout=SHUTDOWN_TIMEOUT_SEC)
|
||||
Reference in New Issue
Block a user