diff --git a/src/fastfood_two/__main__.py b/src/fastfood_two/__main__.py index 42ffa75..7ef397d 100644 --- a/src/fastfood_two/__main__.py +++ b/src/fastfood_two/__main__.py @@ -1,10 +1,15 @@ import uvicorn -if __name__ == "__main__": + +def main(): uvicorn.run( - "fastfood_two.main:app_factory", + "fastfood_two.entrypoint.main:app_factory", host="0.0.0.0", port=8000, factory=True, reload=True, ) + + +if __name__ == "__main__": + main() diff --git a/src/fastfood_two/__pycache__/__main__.cpython-312.pyc b/src/fastfood_two/__pycache__/__main__.cpython-312.pyc index ccd467a..e4770c6 100644 Binary files a/src/fastfood_two/__pycache__/__main__.cpython-312.pyc and b/src/fastfood_two/__pycache__/__main__.cpython-312.pyc differ diff --git a/src/fastfood_two/__pycache__/main.cpython-312.pyc b/src/fastfood_two/__pycache__/main.cpython-312.pyc index 48743b0..2797535 100644 Binary files a/src/fastfood_two/__pycache__/main.cpython-312.pyc and b/src/fastfood_two/__pycache__/main.cpython-312.pyc differ diff --git a/src/fastfood_two/common/__pycache__/logger.cpython-312.pyc b/src/fastfood_two/common/__pycache__/logger.cpython-312.pyc index a7fe038..ccebf13 100644 Binary files a/src/fastfood_two/common/__pycache__/logger.cpython-312.pyc and b/src/fastfood_two/common/__pycache__/logger.cpython-312.pyc differ diff --git a/src/fastfood_two/common/logger.py b/src/fastfood_two/common/logger.py index bfca042..8343b5c 100644 --- a/src/fastfood_two/common/logger.py +++ b/src/fastfood_two/common/logger.py @@ -1,7 +1,9 @@ import logging -def logger_configure(level=logging.INFO) -> None: +def configure_logger(level=logging.INFO) -> None: + """Configure the logger output string.""" + logging.basicConfig( level=level, datefmt="%Y-%m-%d %H:%M:%S", diff --git a/src/fastfood_two/entrypoint/__init__.py b/src/fastfood_two/entrypoint/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/fastfood_two/entrypoint/__pycache__/__init__.cpython-312.pyc b/src/fastfood_two/entrypoint/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..cafce38 Binary files /dev/null and b/src/fastfood_two/entrypoint/__pycache__/__init__.cpython-312.pyc differ diff --git a/src/fastfood_two/entrypoint/__pycache__/dependencies.cpython-312.pyc b/src/fastfood_two/entrypoint/__pycache__/dependencies.cpython-312.pyc new file mode 100644 index 0000000..50f5ea8 Binary files /dev/null and b/src/fastfood_two/entrypoint/__pycache__/dependencies.cpython-312.pyc differ diff --git a/src/fastfood_two/entrypoint/__pycache__/error_handlers.cpython-312.pyc b/src/fastfood_two/entrypoint/__pycache__/error_handlers.cpython-312.pyc new file mode 100644 index 0000000..b1fc053 Binary files /dev/null and b/src/fastfood_two/entrypoint/__pycache__/error_handlers.cpython-312.pyc differ diff --git a/src/fastfood_two/entrypoint/__pycache__/main.cpython-312.pyc b/src/fastfood_two/entrypoint/__pycache__/main.cpython-312.pyc new file mode 100644 index 0000000..590684b Binary files /dev/null and b/src/fastfood_two/entrypoint/__pycache__/main.cpython-312.pyc differ diff --git a/src/fastfood_two/entrypoint/dependencies.py b/src/fastfood_two/entrypoint/dependencies.py new file mode 100644 index 0000000..6d951c4 --- /dev/null +++ b/src/fastfood_two/entrypoint/dependencies.py @@ -0,0 +1,14 @@ +import logging + +from fastapi import FastAPI + +from fastfood_two.common.logger import configure_logger + +logger = logging.getLogger(__name__) +configure_logger(level=logging.INFO) + + +def init_dependencies(app: FastAPI) -> None: + """Initialize FastAPI dependencies.""" + + logger.info("Dependencies initialized") diff --git a/src/fastfood_two/entrypoint/error_handlers.py b/src/fastfood_two/entrypoint/error_handlers.py new file mode 100644 index 0000000..dd3a370 --- /dev/null +++ b/src/fastfood_two/entrypoint/error_handlers.py @@ -0,0 +1,6 @@ +from fastapi import FastAPI + + +def init_errorhandlers(app: FastAPI) -> None: + """Initialize FastAPI error handlers.""" + pass diff --git a/src/fastfood_two/entrypoint/main.py b/src/fastfood_two/entrypoint/main.py new file mode 100644 index 0000000..1558065 --- /dev/null +++ b/src/fastfood_two/entrypoint/main.py @@ -0,0 +1,50 @@ +import logging +from contextlib import asynccontextmanager +from typing import AsyncGenerator + +from fastapi import FastAPI + +from fastfood_two.common.logger import configure_logger +from fastfood_two.entrypoint.dependencies import init_dependencies +from fastfood_two.entrypoint.error_handlers import init_errorhandlers + +logger = logging.getLogger(__name__) +configure_logger(level=logging.INFO) + + +@asynccontextmanager +async def app_lifespan(app: FastAPI) -> AsyncGenerator: + """Application lifespan. + + This function is called when the application starts and stops. + Do not use this method directly, use the FastAPI `lifespan` instead. + + :param app: FastAPI application + :type app: FastAPI + + """ + + logger.info("Application lifespan started") + + init_dependencies(app) + init_errorhandlers(app) + + yield + + logger.info("Application lifespan stopped") + + +def app_factory() -> FastAPI: + """Create a FastAPI application. + + Create a FastAPI application and initialize all dependencies. + + :return: FastAPI + :rtype: FastAPI + """ + + app = FastAPI( + lifespan=app_lifespan, + ) + + return app diff --git a/src/fastfood_two/main.py b/src/fastfood_two/main.py deleted file mode 100644 index 883c9db..0000000 --- a/src/fastfood_two/main.py +++ /dev/null @@ -1,27 +0,0 @@ -import logging -from contextlib import asynccontextmanager -from typing import AsyncGenerator - -from fastapi import FastAPI - -from fastfood_two.common.logger import logger_configure - -logger = logging.getLogger(__name__) - - -@asynccontextmanager -async def app_lifespan(app: FastAPI) -> AsyncGenerator: - """Application lifespan.""" - logger_configure(level=logging.INFO) - logger.info("Application started") - yield - logger.info("Application stopped") - - -def app_factory() -> FastAPI: - """Create a FastAPI application.""" - - app = FastAPI( - lifespan=app_lifespan, - ) - return app