From 8f2db50067072cad79c63227cf5b974b3d9bc2ca Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Fri, 16 Aug 2024 12:21:14 +0000 Subject: [PATCH] ADD logger lifespan init deps&errors --- src/fastfood_two/__main__.py | 9 +++- .../__pycache__/__main__.cpython-312.pyc | Bin 376 -> 512 bytes .../__pycache__/main.cpython-312.pyc | Bin 1164 -> 1160 bytes .../common/__pycache__/logger.cpython-312.pyc | Bin 558 -> 597 bytes src/fastfood_two/common/logger.py | 4 +- src/fastfood_two/entrypoint/__init__.py | 0 .../__pycache__/__init__.cpython-312.pyc | Bin 0 -> 165 bytes .../__pycache__/dependencies.cpython-312.pyc | Bin 0 -> 721 bytes .../error_handlers.cpython-312.pyc | Bin 0 -> 402 bytes .../__pycache__/main.cpython-312.pyc | Bin 0 -> 1717 bytes src/fastfood_two/entrypoint/dependencies.py | 14 +++++ src/fastfood_two/entrypoint/error_handlers.py | 6 +++ src/fastfood_two/entrypoint/main.py | 50 ++++++++++++++++++ src/fastfood_two/main.py | 27 ---------- 14 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 src/fastfood_two/entrypoint/__init__.py create mode 100644 src/fastfood_two/entrypoint/__pycache__/__init__.cpython-312.pyc create mode 100644 src/fastfood_two/entrypoint/__pycache__/dependencies.cpython-312.pyc create mode 100644 src/fastfood_two/entrypoint/__pycache__/error_handlers.cpython-312.pyc create mode 100644 src/fastfood_two/entrypoint/__pycache__/main.cpython-312.pyc create mode 100644 src/fastfood_two/entrypoint/dependencies.py create mode 100644 src/fastfood_two/entrypoint/error_handlers.py create mode 100644 src/fastfood_two/entrypoint/main.py delete mode 100644 src/fastfood_two/main.py 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 ccd467a705a8a3ce4d1e967843c7b9658e468e3c..e4770c6ed751f7efab94c746d5c081f94d12c6e0 100644 GIT binary patch literal 512 zcmY*WJxjzu5S>l-lHeIVQ3M4My9-Dn+Nk(d*$7rvHX&qlT=bF+o1BuX6cqgj{sz(i z;195~iqTFG><$kstenIg=q~eS=I!Inviqp(BY&A{IBmfRPRy z0+TM_ka!Z4kDCBOlfWZ)m6SkAQex1LqThEbwoEFbj4@b6xPgR}EW*)n&XB;&2t$8H z8NILF)qW4q8w_Q?*K9d)(&C)i$tgFzFcE3Q{V*{D#}C(?D6(6Qn{bi#)Frdr{?0tW zfkAWH=5dnKh>IjwD^;!tufrW?NV(cQ@m(&$To&E%76!%K?=`E<1J8>571t78#I3*M ztysAK3EM8NVcTYu=Cmk^ogu7m5on+)s>ne>Zr)I zp@ks}4T;jAU_&SHY+Do`Rw~6PR^Qr2z*)EBZHWf9#jC~(2z{9l@&aR@ib|CJ6wuM@ S-jh7@Ots71z6^A>wDxb-m5h~)nj9}dT)$f!@$tEdnSy!o@l~>E ziNz&p`S~gFCFS`O&p3!M-(oK<%S_HM$^$9OOUzA;kDn~Y=6uud$?m NU}j*ED&hfZ0suJ;J&gbW diff --git a/src/fastfood_two/__pycache__/main.cpython-312.pyc b/src/fastfood_two/__pycache__/main.cpython-312.pyc index 48743b036d770d433f69ef773dfdf352b13c8879..2797535f7d77fa26ffddcd6605cb0949456cb4b9 100644 GIT binary patch delta 326 zcmeC-?BL`*&CAQh00e~``_tkl@~WCeF{Ci0Fy%1jGDR^lg4oPC%(*O4EMPWE4r?x3 z6dRb$n!}#U5yipCz{HTsFfl_>pDBecl`Vxml_iZOg`Xh~fc?v86GkaJH~S z@m4Zwa=ioz`%T<#BBIH7OCUKvFD)~@v?w(`CqF$swP+IK2{BQ}f`XjPcpu(TuW_C7ER>n=@N6 zN=zPa#^ET!EBZs zwkS5BJZlbnE=LpxBLfpdD%->iMLot8wp7*>_EhFHmK2T_men9*fgp+pD8`z`l)~A< z62)7|qRI6VBs_74iKw3@<1K-l{PgtHqWI+eytK^p(xTK!j3*?-6&wo+ax#+>OEUBG z6ml}tQi}@`^Yj!Z-(y@azzx)0YzQP?F#Kp>_`)`ME|Vn79Tv_zlXo#iE66K27o{ea zq$(sTxFr^sI0kqsBqGcL8ez{S2RB2#)z$o&8iIq`(hWiHwAhm+;6NviE%)lfH7XKi? R#>hV*{0jq+DiQ@c4*;2|UWotz diff --git a/src/fastfood_two/common/__pycache__/logger.cpython-312.pyc b/src/fastfood_two/common/__pycache__/logger.cpython-312.pyc index a7fe038427fbdafaa6867305a6214c31aa503446..ccebf13ee6ecff66f3447610eb5bbad08623c499 100644 GIT binary patch delta 122 zcmZ3-a+QVmG%qg~0}zB8?N8I6$U9GiMU%Bk**QNiEi=8eC{>{(BUK?MKRrFQNFl$p zq@c7!p}3?dGcR3l;`#8i;v#qkh@MIZxNfVfy1NHj2fVPjzBnY@!RmxG;= Jt%w^a1^^F$BGv!^ delta 82 zcmcc0vW|uKG%qg~0}$AE?@MEt$UBdPL6gOA;^l_PA&lwDIr-`7sYUV0`FUxX>7_-f fMIfatKwK;dBpMj*uy9X)z?jRy%E(s44HN+Y!G;wP 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 0000000000000000000000000000000000000000..cafce381d5ade22619aec2c2cebc719c81232e96 GIT binary patch literal 165 zcmX@j%ge<81WpF~(?IlN5P=Rpvj9b=GgLBYGWxA#C}INgK7-W!a?#Jo&rQ`Y$TUvY zFG?-Q*H23ljA6uz@BcH9z_3nWIkswOq;Y4*Rs@SmxX;hAH%&uB%XjE;%B@Zn-o5QYhh zDJgs78xL1nm;N2c1J97w+@?g7CESC;QhKF!=UzPWpYfdd1-%pdf)w0O)G-szaabL2 zzZCIhg0M=A3Qk$&U(zo|vZF=ARaRV468g)O9R)|=+yagDm%GQipF6is(e0Tcfwv}Y z?T)fk+FCm*sL)p;yQ@xmG0$_(dh>$_{fkH^CDr5l{$(=^S(KA76pns_`h%S(wfZ|C zT8a_t3ND&=bB>1@xhJlvR-eDTuV89I2!DX9XW-@;*!<~qUtRy^biX*=ldZ4L#*y_M dv|k@g4&NM}f{iIMVee#f3RHSqo$FK!`~h3Yv;qJC literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..b1fc0533cc40dfade8277e8c65bf9c66237e19e9 GIT binary patch literal 402 zcmY*Vu};G<5VaGxNfBLGyF_eRun;RkVgZ$*V(t<}v7=f_V(XF$r4vH@L-_>YC-?yV zAW|hHCbmdn>clx}5!`U^-TCf4+wbG?0l|1ZeM=t@KW(uOwj*7DWK9GSG$T2kQ%Zy* zsc;2LnHW5>xl^-n_+`gK+IUxSSzX>v8#gwl8G@{qP|Ol)kN<>=yt5W-Qq#~`9Zw5Y zsW?+B8SE4S37{cZ#D&Ntlqa(n)Cap$TD4$%!xuW2+^DmJgEX3_Xq{>;qUuib5|aN7 zSyWIPt%{2G%F(Z~shcB({!!oGw_$`Xz1;xGJ^5gMhlGS~<5{QJD6r4y=fJbf{wDaC nw*Gmp#WIr@aEPV7Z#lu#x|GrlnY2TPj-IYtg7D0~E%eJjZewJR literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..590684bd74d2cd13f6667e7c577e95d8993b922f GIT binary patch literal 1717 zcmah}&5ImG6tC*3>G|5(kBO^cqR@dgFhOPn5oHl^Tuq28tDqqCWzu$6&vdA+ZmMfs zXIWvDfZkj$+58EHB!7b!Z(bBPh;0@GH=wt{*@NcftD5PVSxK~@U%mdlSFc|E-mChq zS}h|O#f@+Lm>~3<`pHIqqKv);QmqudQ>0**BeQXwMIbGXN-P-vdf zk0?uXP8_GEpt3KxJ2|M%HwQ9jAT+uG$|k}+Rv_w<3jZRA-_c)t=;SQQS3ckTbo2Y=<{@bv zlhu9qkks#!C-;*>a_*Rvj!Ai9zeiesCnl`?tX*iAunXFB=+1<{(FG9DmD8@3(5~Zg zXWEOOKnK2u8ph6-8-h9#=zSFZlOp|>i*5~dlLChYI}nF4YyP5Tbsk@(Sp`)mt_xM! z(->UAVn$ZO5bDBc&V(jS*F-D>m{t_6zT?6JFc41w@+^GGSr9;J4OMFQSDrb^85MJH zV^~BBOM7pPz=j%U(t@gcz`JRA^0KhtL!sD9O~?lf0^V4Z7F4C3IM6wy_4&Ft-6#wr z-W)fOPz6cXPC3l_pgGTm?y~g4<6QM))X82W-HmCfOXb`8Axn#$4tGM@>4>V@kzCoj z^v;+Zb^cpOtiq((d02cUjJ!UhuZr`4DcTYr#4v|3{sk=_p^Hc8`~!6E0ea~Oy?%t= z{Iyj7;;ElX^&d+0n=k)ZTH7;!M$7x#2fJVG-a~6c6B(;RqlTZqd3lIn`g-wCHH~r^ RKD=1POZy+3AeeL>{{Xv2!+8Jz literal 0 HcmV?d00001 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