mirror of
https://github.com/ijaric/voice_assistant.git
synced 2025-05-24 22:43:26 +00:00
add app settings
This commit is contained in:
parent
659271d2b0
commit
832726d8ae
|
@ -1,36 +1,142 @@
|
||||||
|
import dataclasses
|
||||||
import logging
|
import logging
|
||||||
|
import logging.config as logging_config
|
||||||
|
import typing
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
import lib.app.errors as app_errors
|
||||||
import lib.app.settings as app_settings
|
import lib.app.settings as app_settings
|
||||||
|
import lib.app.split_settings as app_split_settings
|
||||||
|
import lib.api.v1.handlers as api_v1_handlers
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
def __init__(self) -> None:
|
def __init__(
|
||||||
self.settings = app_settings
|
self,
|
||||||
self.logger = logging.getLogger(__name__)
|
settings: app_settings.Settings,
|
||||||
self.producer = None
|
fastapi_app: fastapi.FastAPI,
|
||||||
|
disposable_resources: list[DisposableResource],
|
||||||
|
) -> None:
|
||||||
|
self._settings = settings
|
||||||
|
self._fastapi_app = fastapi_app
|
||||||
|
self._disposable_resources = disposable_resources
|
||||||
|
# self.logger = logging.getLogger(__name__)
|
||||||
|
# self.producer = None
|
||||||
|
|
||||||
def create_app(self) -> fastapi.FastAPI:
|
# def create_app(self) -> fastapi.FastAPI:
|
||||||
app = fastapi.FastAPI(
|
# app = fastapi.FastAPI(
|
||||||
title="FastAPI",
|
# title=self._settings.app.title,
|
||||||
version="0.1.0",
|
# version=self._settings.app.version,
|
||||||
docs_url="/api/openapi",
|
# docs_url=self._settings.app.docs_url,
|
||||||
openapi_url="/api/openapi.json",
|
# openapi_url=self._settings.app.openapi_url,
|
||||||
|
# default_response_class=fastapi.responses.ORJSONResponse,
|
||||||
|
# )
|
||||||
|
#
|
||||||
|
# @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")
|
||||||
|
#
|
||||||
|
# return app
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_settings(cls, settings: app_settings.Settings) -> typing.Self:
|
||||||
|
# Logging
|
||||||
|
|
||||||
|
# logging.basicConfig(
|
||||||
|
# level=settings.LOGS_MIN_LEVEL,
|
||||||
|
# format=settings.LOGS_FORMAT,
|
||||||
|
# )
|
||||||
|
logging_config.dictConfig(app_split_settings.get_logging_config(**settings.logger.model_dump()))
|
||||||
|
|
||||||
|
logger.info("Initializing application")
|
||||||
|
disposable_resources = []
|
||||||
|
|
||||||
|
# Global clients
|
||||||
|
|
||||||
|
logger.info("Initializing global clients")
|
||||||
|
|
||||||
|
# Clients
|
||||||
|
|
||||||
|
logger.info("Initializing clients")
|
||||||
|
|
||||||
|
# Repositories
|
||||||
|
|
||||||
|
logger.info("Initializing repositories")
|
||||||
|
|
||||||
|
# Caches
|
||||||
|
|
||||||
|
logger.info("Initializing caches")
|
||||||
|
|
||||||
|
# Services
|
||||||
|
|
||||||
|
logger.info("Initializing services")
|
||||||
|
|
||||||
|
# Handlers
|
||||||
|
|
||||||
|
logger.info("Initializing handlers")
|
||||||
|
# liveness_probe_handler = health_handlers.LivenessProbeHandler()
|
||||||
|
|
||||||
|
logger.info("Creating application")
|
||||||
|
# aio_app = aiohttp_web.Application()
|
||||||
|
|
||||||
|
fastapi_app = fastapi.FastAPI(
|
||||||
|
title=settings.app.title,
|
||||||
|
version=settings.app.version,
|
||||||
|
docs_url=settings.app.docs_url,
|
||||||
|
openapi_url=settings.app.openapi_url,
|
||||||
default_response_class=fastapi.responses.ORJSONResponse,
|
default_response_class=fastapi.responses.ORJSONResponse,
|
||||||
)
|
)
|
||||||
|
|
||||||
# app.include_router(api_handlers.user_router, prefix="/api/v1/users", tags=["users"])
|
# Routes
|
||||||
# app.include_router(api_handlers.movie_router, prefix="/api/v1/movies", tags=["movies"])
|
fastapi_app.include_router(api_v1_handlers.health_router, prefix="/api/v1/health", tags=["health"])
|
||||||
|
|
||||||
@app.on_event("startup")
|
application = Application(
|
||||||
async def startup_event():
|
settings=settings,
|
||||||
self.logger.info("Starting server")
|
fastapi_app=fastapi_app,
|
||||||
|
disposable_resources=disposable_resources,
|
||||||
|
)
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
logger.info("Initializing application finished")
|
||||||
async def shutdown_event():
|
|
||||||
self.logger.info("Shutting down server")
|
|
||||||
|
|
||||||
return app
|
return application
|
||||||
|
|
||||||
|
async def start(self) -> None:
|
||||||
|
try:
|
||||||
|
config = uvicorn.Config(
|
||||||
|
app=self._fastapi_app,
|
||||||
|
host=self._settings.api.host,
|
||||||
|
port=self._settings.api.port,
|
||||||
|
)
|
||||||
|
server = uvicorn.Server(config)
|
||||||
|
await server.serve()
|
||||||
|
except BaseException as unexpected_error:
|
||||||
|
logger.exception("FastAPI failed to start")
|
||||||
|
raise app_errors.StartServerError("FastAPI failed to start") from unexpected_error
|
||||||
|
|
||||||
|
async def dispose(self) -> None:
|
||||||
|
logger.info("Application is shutting down...")
|
||||||
|
dispose_errors = []
|
||||||
|
|
||||||
|
for resource in self._disposable_resources:
|
||||||
|
logger.info("Disposing %s...", resource.name)
|
||||||
|
try:
|
||||||
|
await resource.dispose_callback
|
||||||
|
except Exception as unexpected_error:
|
||||||
|
dispose_errors.append(unexpected_error)
|
||||||
|
logger.exception("Failed to dispose %s", resource.name)
|
||||||
|
else:
|
||||||
|
logger.info("%s has been disposed", resource.name)
|
||||||
|
|
||||||
|
if len(dispose_errors) != 0:
|
||||||
|
logger.error("Application has shut down with errors")
|
||||||
|
raise app_errors.DisposeError("Application has shut down with errors, see logs above")
|
||||||
|
|
||||||
|
logger.info("Application has successfully shut down")
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import logging.config as logging_config
|
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
import pydantic_settings
|
import pydantic_settings
|
||||||
|
|
||||||
|
@ -17,10 +15,3 @@ class Settings(pydantic_settings.BaseSettings):
|
||||||
project: app_split_settings.ProjectSettings = pydantic.Field(
|
project: app_split_settings.ProjectSettings = pydantic.Field(
|
||||||
default_factory=lambda: app_split_settings.ProjectSettings()
|
default_factory=lambda: app_split_settings.ProjectSettings()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
settings = Settings() # todo Вынести в инициализацию
|
|
||||||
|
|
||||||
logging_config.dictConfig( # todo Вынести в инициализацию
|
|
||||||
app_split_settings.get_logging_config(**settings.logger.model_dump())
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user