mirror of
https://github.com/ijaric/voice_assistant.git
synced 2025-05-24 14:33:26 +00:00
Запуск через lifespan
This commit is contained in:
parent
0cc0b7f0bc
commit
37f3bce194
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import logging.config as logging_config
|
import logging.config as logging_config
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
|
|
||||||
|
@ -18,6 +19,13 @@ class Application:
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
self.producer = None
|
self.producer = None
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(self, app: fastapi.FastAPI):
|
||||||
|
self.logger.info("Starting server")
|
||||||
|
yield
|
||||||
|
# Clean up the ML models and release the resources
|
||||||
|
self.logger.info("Shutting down server")
|
||||||
|
|
||||||
def setup_application(self, app: fastapi.FastAPI) -> fastapi.FastAPI:
|
def setup_application(self, app: fastapi.FastAPI) -> fastapi.FastAPI:
|
||||||
logger.info("Initializing application")
|
logger.info("Initializing application")
|
||||||
|
|
||||||
|
@ -58,17 +66,10 @@ class Application:
|
||||||
docs_url="/api/openapi",
|
docs_url="/api/openapi",
|
||||||
openapi_url="/api/openapi.json",
|
openapi_url="/api/openapi.json",
|
||||||
default_response_class=fastapi.responses.ORJSONResponse,
|
default_response_class=fastapi.responses.ORJSONResponse,
|
||||||
|
lifespan=self.lifespan,
|
||||||
)
|
)
|
||||||
app = self.setup_application(app)
|
app = self.setup_application(app)
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup_event():
|
|
||||||
self.logger.info("Starting server")
|
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
|
||||||
async def shutdown_event():
|
|
||||||
self.logger.info("Shutting down server")
|
|
||||||
|
|
||||||
app.state.settings = self.settings
|
app.state.settings = self.settings
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
|
@ -21,6 +21,4 @@ class Settings(pydantic_settings.BaseSettings):
|
||||||
|
|
||||||
settings = Settings() # todo Вынести в инициализацию
|
settings = Settings() # todo Вынести в инициализацию
|
||||||
|
|
||||||
logging_config.dictConfig(
|
logging_config.dictConfig(app_split_settings.get_logging_config(**settings.logger.model_dump()))
|
||||||
app_split_settings.get_logging_config(**settings.logger.model_dump())
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user