From b097f3c13ddfa51551bce9ca9344c796e5cea9cb Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Thu, 23 Feb 2023 23:42:50 -0800 Subject: deploy: add krakend api gateway --- bot.py | 8 ++-- contrib/docker-compose.yaml | 11 +++++- contrib/krakend.json | 90 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 contrib/krakend.json diff --git a/bot.py b/bot.py index b44f9df..bcfab8f 100644 --- a/bot.py +++ b/bot.py @@ -8,7 +8,7 @@ from config import BOT_TOKEN, TELEGRAM_WEBHOOK_URL, HEALTHCHECK_URL, FEDI_LOGIN_ import uvicorn from starlette.applications import Starlette from starlette.requests import Request -from starlette.responses import PlainTextResponse, Response +from starlette.responses import PlainTextResponse, Response, JSONResponse from starlette.routing import Route from telegram import Update from telegram.ext import ( @@ -188,12 +188,12 @@ async def main() -> None: await application.bot.set_webhook(url=f"{BOT_DOMAIN}{TELEGRAM_WEBHOOK_URL}") # Set up webserver - async def telegram_webhook(request: Request) -> Response: + async def telegram_webhook(request: Request) -> JSONResponse: """Handle incoming Telegram updates by putting them into the `update_queue`""" await application.update_queue.put( Update.de_json(data=await request.json(), bot=application.bot) ) - return Response() + return JSONResponse({'OK': 200}) async def fedi_oauth_login_callback(request: Request) -> PlainTextResponse: """ @@ -221,7 +221,7 @@ async def main() -> None: routes=[ Route(TELEGRAM_WEBHOOK_URL, telegram_webhook, methods=["POST"]), Route(HEALTHCHECK_URL, healthcheck, methods=["GET"]), - Route(FEDI_LOGIN_CALLBACK_URL, fedi_oauth_login_callback, methods=["POST", "GET"]), + Route(FEDI_LOGIN_CALLBACK_URL, fedi_oauth_login_callback, methods=["GET"]), ] ) webserver = uvicorn.Server( diff --git a/contrib/docker-compose.yaml b/contrib/docker-compose.yaml index 7ba8c2d..3ca7981 100644 --- a/contrib/docker-compose.yaml +++ b/contrib/docker-compose.yaml @@ -1,6 +1,6 @@ version: '3' services: - checkin.bot: + checkinbot: image: "clarkzjw/checkin.bot:latest" restart: always volumes: @@ -9,5 +9,14 @@ services: ports: - "8080:8080" + krakend: + image: "devopsfaith/krakend:latest" + restart: always + ports: + - "8081:8080" + - "9091:9091" + volumes: + - "./krakend.json:/etc/krakend/krakend.json" + volumes: sqlite3: diff --git a/contrib/krakend.json b/contrib/krakend.json new file mode 100644 index 0000000..1f89340 --- /dev/null +++ b/contrib/krakend.json @@ -0,0 +1,90 @@ +{ + "$schema": "https://www.krakend.io/schema/v3.json", + "version": 3, + "name": "Checkin.bot API", + "extra_config": { + "telemetry/opencensus": { + "sample_rate": 100, + "reporting_period": 0, + "exporters": { + "prometheus": { + "port": 9091, + "namespace": "krakend", + "tag_host": false, + "tag_path": true, + "tag_method": true, + "tag_statuscode": false + } + } + }, + "telemetry/metrics": { + "collection_time": "60s", + "proxy_disabled": false, + "router_disabled": false, + "backend_disabled": false, + "endpoint_disabled": false, + "listen_address": ":8090" + } + }, + "timeout": "3000ms", + "cache_ttl": "300s", + "output_encoding": "json", + "debug_endpoint": true, + "endpoints": [ + { + "endpoint": "/checkinbot/webhook", + "method": "POST", + "output_encoding": "json", + "backend": [ + { + "url_pattern": "/checkinbot/webhook", + "encoding": "json", + "sd": "static", + "method": "POST", + "host": [ + "http://checkinbot:8080" + ], + "disable_host_sanitize": false + } + ] + }, + { + "endpoint": "/checkinbot/healthcheck", + "method": "GET", + "output_encoding": "string", + "backend": [ + { + "url_pattern": "/checkinbot/healthcheck", + "encoding": "string", + "sd": "static", + "method": "GET", + "host": [ + "http://checkinbot:8080" + ], + "disable_host_sanitize": false + } + ] + }, + { + "endpoint": "/checkinbot/fedi_login_callback", + "method": "GET", + "output_encoding": "string", + "backend": [ + { + "url_pattern": "/checkinbot/fedi_login_callback", + "encoding": "string", + "sd": "static", + "method": "GET", + "disable_host_sanitize": false, + "host": [ + "http://checkinbot:8080" + ] + } + ], + "input_query_strings": [ + "code", + "state" + ] + } + ] +} \ No newline at end of file -- cgit v1.2.3