Moved app part to web package

This commit is contained in:
Alexey Sokolov
2022-02-10 17:21:47 +03:00
parent 125198a0e2
commit 0c83713a17
6 changed files with 65 additions and 30 deletions

View File

@@ -47,7 +47,7 @@ _base_url = getenv('LTLNOTIFIER_BASE_URL')
if not _base_url:
raise ConfigError('virtual environment LTLNOTIFIER_BASE_URL not set')
_port = getenv('LTLNOTIFIER_PORT')
_port = getenv('LTLNOTIFIER_PORT', default='')
_port = int(_port) if _port.isdigit() else 3001
config = Config(teletoken=_teletoken,

View File

@@ -1,40 +1,13 @@
import logging
from aiogram.dispatcher.webhook import configure_app
from aiohttp import web
from config import config
from telegram import telegram_bot, telegram_dispatcher
from web import app
WEBAPP_PORT = 3001
TELEGRAM_WEBHOOK_URL = f'{config.base_url}/{config.teletoken}'
logging.basicConfig(level=logging.INFO)
async def test_route(_):
return web.json_response({'test': 'passed'}, status=200)
async def on_startup(_):
await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL)
async def on_shutdown(_):
logging.warning('Shutting down')
await telegram_bot.delete_webhook()
await telegram_dispatcher.storage.close()
await telegram_dispatcher.storage.wait_closed()
session = await telegram_bot.get_session()
await session.close()
logging.warning('Bye!..')
app = web.Application()
app.add_routes([web.get('/test', test_route)])
app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)
configure_app(telegram_dispatcher, app, '/'+config.teletoken)
if __name__ == '__main__':
web.run_app(app, port=WEBAPP_PORT)
web.run_app(app, port=config.port)

View File

@@ -1,4 +1,5 @@
from aiogram import Bot, Dispatcher
from aiogram.dispatcher.webhook import SendMessage
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from config import config
@@ -9,4 +10,9 @@ telegram_dispatcher = Dispatcher(telegram_bot)
telegram_dispatcher.middleware.setup(LoggingMiddleware())
@telegram_dispatcher.message_handler(commands=['test'])
async def test_telegram(message):
return SendMessage(message.chat.id, 'passed')
__all__ = ('telegram_bot', telegram_dispatcher)

View File

@@ -0,0 +1,34 @@
import logging
from aiogram.dispatcher.webhook import configure_app
from aiohttp import web
from config import config
from telegram import telegram_bot, telegram_dispatcher
from .routes import routes, TELEGRAM_WEBHOOK_ROUTE
TELEGRAM_WEBHOOK_URL = f'{config.base_url}{TELEGRAM_WEBHOOK_ROUTE}'
async def on_startup(_):
await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL)
async def on_shutdown(_):
logging.warning('Shutting down')
await telegram_bot.delete_webhook()
await telegram_dispatcher.storage.close()
await telegram_dispatcher.storage.wait_closed()
session = await telegram_bot.get_session()
await session.close()
logging.warning('Bye!..')
app = web.Application()
app.add_routes(routes)
app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)
configure_app(telegram_dispatcher, app, TELEGRAM_WEBHOOK_ROUTE)
__all__ = ('app',)

View File

@@ -0,0 +1,14 @@
from aiohttp import web
from config import config
from .views import test_route
TELEGRAM_WEBHOOK_ROUTE = '/' + config.teletoken
routes = [
web.get('/test', test_route)
]
__all__ = ('TELEGRAM_WEBHOOK_ROUTE', 'routes')

View File

@@ -0,0 +1,8 @@
from aiohttp import web
async def test_route(_):
return web.json_response({'test': 'passed'}, status=200)
__all__ = ('test_route',)