From 94bef74996a3b987bc7cf6148fb5cbf141dc5719 Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev Date: Tue, 16 Aug 2022 11:40:29 +0300 Subject: [PATCH] app with uvicorn --- app/core/bot.py | 2 -- app/main.py | 20 +++++++++++--------- app/settings.py | 2 +- poetry.lock | 46 ++++++++++++++++++++++++++++++++++------------ pyproject.toml | 1 + 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/app/core/bot.py b/app/core/bot.py index 5012deb..6f54369 100644 --- a/app/core/bot.py +++ b/app/core/bot.py @@ -5,7 +5,6 @@ from aiogram.contrib.middlewares.logging import LoggingMiddleware from aiogram.dispatcher import Dispatcher from aiogram.dispatcher.webhook import SendMessage from aiogram.utils.callback_data import CallbackData -from app.core.logger import logger from app.core.parse_web import ( configure_firefox_driver, download_gecko_driver, @@ -71,7 +70,6 @@ async def office_home( @dispatcher.message_handler(commands=['chatid']) async def chat_id(message: types.Message) -> SendMessage: - logger.info('Hello World') return SendMessage(message.chat.id, message.chat.id) diff --git a/app/main.py b/app/main.py index 11df2d3..1b194ac 100644 --- a/app/main.py +++ b/app/main.py @@ -57,7 +57,7 @@ def bot_webhook() -> None: ) -async def index(request: web.Request) -> web.Response: +async def webhook(request: web.Request) -> web.Response: data = await request.json() Bot.set_current(dispatcher.bot) Dispatcher.set_current(dispatcher) @@ -66,17 +66,19 @@ async def index(request: web.Request) -> web.Response: return web.Response(status=HTTPStatus.OK) -async def create_app() -> web.Application: - app = web.Application() - app.router.add_post('/', index) - return app +def create_app() -> web.Application: + application = web.Application() + application.router.add_post('/', webhook) + return application if __name__ == '__main__': + import uvicorn if START_WITH_WEBHOOK: - bot_webhook() - else: - # bot_polling() # type: ignore + # bot_webhook() # type: ignore app = create_app() - web.run_app(app=app, host='localhost', port=8084) + uvicorn.run(app=app, host=WEBAPP_HOST, port=WEBAPP_PORT) + else: + bot_polling() + diff --git a/app/settings.py b/app/settings.py index c9a09d2..3216476 100644 --- a/app/settings.py +++ b/app/settings.py @@ -23,7 +23,7 @@ WEBHOOK_PATH = config('WEBHOOK_PATH', default='') WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}" # webserver settings -WEBAPP_HOST = config('WEBAPP_HOST', default='localhost') # or ip +WEBAPP_HOST = config('WEBAPP_HOST', default='127.0.0.1') # or ip WEBAPP_PORT = config('WEBAPP_PORT', cast=int, default=8084) START_WITH_WEBHOOK = config('START_WITH_WEBHOOK', cast=bool, default=False) diff --git a/poetry.lock b/poetry.lock index 73f1a1c..5745a51 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,6 +1,6 @@ [[package]] name = "aiogram" -version = "2.22" +version = "2.22.1" description = "Is a pretty simple and fully asynchronous framework for Telegram Bot API" category = "main" optional = false @@ -12,8 +12,8 @@ Babel = ">=2.9.1,<2.10.0" certifi = ">=2021.10.8" [package.extras] +fast = ["uvloop (>=0.16.0,<0.17.0)", "ujson (>=1.35)"] proxy = ["aiohttp-socks (>=0.5.3,<0.6.0)"] -fast = ["ujson (>=1.35)", "uvloop (>=0.16.0,<0.17.0)"] [[package]] name = "aiohttp" @@ -127,7 +127,7 @@ python-versions = ">=3.8" [[package]] name = "asttokens" -version = "2.0.7" +version = "2.0.8" description = "Annotate AST trees with source code positions" category = "dev" optional = false @@ -289,7 +289,7 @@ unicode_backport = ["unicodedata2"] name = "click" version = "8.1.3" description = "Composable command line interface toolkit" -category = "dev" +category = "main" optional = false python-versions = ">=3.7" @@ -1052,12 +1052,15 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" [[package]] name = "pygments" -version = "2.12.0" +version = "2.13.0" description = "Pygments is a syntax highlighting package written in Python." category = "dev" optional = false python-versions = ">=3.6" +[package.extras] +plugins = ["importlib-metadata"] + [[package]] name = "pyopenssl" version = "22.0.0" @@ -1612,6 +1615,21 @@ brotli = ["brotlicffi (>=0.8.0)", "brotli (>=1.0.9)", "brotlipy (>=0.6.0)"] secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] +[[package]] +name = "uvicorn" +version = "0.18.2" +description = "The lightning-fast ASGI server." +category = "main" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +click = ">=7.0" +h11 = ">=0.8" + +[package.extras] +standard = ["websockets (>=10.0)", "httptools (>=0.4.0)", "watchfiles (>=0.13)", "python-dotenv (>=0.13)", "PyYAML (>=5.1)", "uvloop (>=0.14.0,!=0.15.0,!=0.15.1)", "colorama (>=0.4)"] + [[package]] name = "virtualenv" version = "20.16.3" @@ -1694,12 +1712,12 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "f6a0f7174a72427f856464538c02eb4b909b78d3ed94afda1f44ae996749c210" +content-hash = "ab6bb9d94e7eaa3b95756c2f584db33ba1635cca6646f9a00ef437a0142743bc" [metadata.files] aiogram = [ - {file = "aiogram-2.22-py3-none-any.whl", hash = "sha256:af5eda881f72d9ad378d93e7c084c11daf6d20834a1799eb7a656b516420b734"}, - {file = "aiogram-2.22.tar.gz", hash = "sha256:92bef502b2f14b58fef2b534bc90319e0789fcfbd63e8d91c719236f1f33a3f7"}, + {file = "aiogram-2.22.1-py3-none-any.whl", hash = "sha256:46b38f7e3c3057ed1eb6c46225f50f0a74e3285937a61cb02d708ec200672f34"}, + {file = "aiogram-2.22.1.tar.gz", hash = "sha256:38bc3fa59b797e444c576d927650abff845620a579b9579ec85cd0a4e19b90a8"}, ] aiohttp = [ {file = "aiohttp-3.8.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:1ed0b6477896559f17b9eaeb6d38e07f7f9ffe40b9f0f9627ae8b9926ae260a8"}, @@ -1804,8 +1822,8 @@ astpretty = [ {file = "astpretty-3.0.0.tar.gz", hash = "sha256:b08c95f32e5994454ea99882ff3c4a0afc8254c38998a0ed4b479dba448dc581"}, ] asttokens = [ - {file = "asttokens-2.0.7-py2.py3-none-any.whl", hash = "sha256:f5589ef8518f73dd82c15e1c19f795d8a62c133485e557c04443d4a1a730cf9f"}, - {file = "asttokens-2.0.7.tar.gz", hash = "sha256:8444353e4e2a99661c8dfb85ec9c02eedded08f0006234bff7db44a06840acc2"}, + {file = "asttokens-2.0.8-py2.py3-none-any.whl", hash = "sha256:e3305297c744ae53ffa032c45dc347286165e4ffce6875dc662b205db0623d86"}, + {file = "asttokens-2.0.8.tar.gz", hash = "sha256:c61e16246ecfb2cde2958406b4c8ebc043c9e6d73aaa83c941673b35e5d3a76b"}, ] async-generator = [ {file = "async_generator-1.10-py3-none-any.whl", hash = "sha256:01c7bf666359b4967d2cda0000cc2e4af16a0ae098cbffcb8472fb9e8ad6585b"}, @@ -2448,8 +2466,8 @@ pyflakes = [ {file = "pyflakes-2.3.1.tar.gz", hash = "sha256:f5bc8ecabc05bb9d291eb5203d6810b49040f6ff446a756326104746cc00c1db"}, ] pygments = [ - {file = "Pygments-2.12.0-py3-none-any.whl", hash = "sha256:dc9c10fb40944260f6ed4c688ece0cd2048414940f1cea51b8b226318411c519"}, - {file = "Pygments-2.12.0.tar.gz", hash = "sha256:5eb116118f9612ff1ee89ac96437bb6b49e8f04d8a13b514ba26f620208e26eb"}, + {file = "Pygments-2.13.0-py3-none-any.whl", hash = "sha256:f643f331ab57ba3c9d89212ee4a2dabc6e94f117cf4eefde99a0574720d14c42"}, + {file = "Pygments-2.13.0.tar.gz", hash = "sha256:56a8508ae95f98e2b9bdf93a6be5ae3f7d8af858b43e02c5a2ff083726be40c1"}, ] pyopenssl = [ {file = "pyOpenSSL-22.0.0-py2.py3-none-any.whl", hash = "sha256:ea252b38c87425b64116f808355e8da644ef9b07e429398bfece610f893ee2e0"}, @@ -2687,6 +2705,10 @@ urllib3 = [ {file = "urllib3-1.26.11-py2.py3-none-any.whl", hash = "sha256:c33ccba33c819596124764c23a97d25f32b28433ba0dedeb77d873a38722c9bc"}, {file = "urllib3-1.26.11.tar.gz", hash = "sha256:ea6e8fb210b19d950fab93b60c9009226c63a28808bc8386e05301e25883ac0a"}, ] +uvicorn = [ + {file = "uvicorn-0.18.2-py3-none-any.whl", hash = "sha256:c19a057deb1c5bb060946e2e5c262fc01590c6529c0af2c3d9ce941e89bc30e0"}, + {file = "uvicorn-0.18.2.tar.gz", hash = "sha256:cade07c403c397f9fe275492a48c1b869efd175d5d8a692df649e6e7e2ed8f4e"}, +] virtualenv = [ {file = "virtualenv-20.16.3-py2.py3-none-any.whl", hash = "sha256:4193b7bc8a6cd23e4eb251ac64f29b4398ab2c233531e66e40b19a6b7b0d30c1"}, {file = "virtualenv-20.16.3.tar.gz", hash = "sha256:d86ea0bb50e06252d79e6c241507cb904fcd66090c3271381372d6221a3970f9"}, diff --git a/pyproject.toml b/pyproject.toml index 071f242..59a975f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ loguru = "^0.6.0" python-decouple = "^3.6" apscheduler = "^3.9.1" SQLAlchemy = {version = "^1.4", extras = ["mypy", "asyncio"]} +uvicorn = "^0.18" factory-boy = "^3.2.1" Faker = "^14.0.0"