From fd15ba829f70ce8dd8935f210fa437f96e737d6a Mon Sep 17 00:00:00 2001 From: Myst33d Date: Wed, 11 May 2022 22:59:01 +0600 Subject: [PATCH] Redo Heroku stuff --- Dockerfile | 16 ++++++++++++++++ Procfile | 2 -- app.json | 14 +++++++++++--- bot.py | 19 ++++++++++++++++++- config.py | 10 +++++----- heroku.yml | 3 +++ heroku_startup.sh | 17 +++++++++++++++++ 7 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 Dockerfile delete mode 100644 Procfile create mode 100644 heroku.yml create mode 100755 heroku_startup.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5044c9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM ubuntu:latest + +WORKDIR /opt +COPY . . + +RUN apt update +RUN apt upgrade -y +RUN apt install -y python3 python3-pip wget + +RUN pip install -r requirements.txt + +RUN wget https://github.com/caddyserver/caddy/releases/download/v2.5.1/caddy_2.5.1_linux_amd64.tar.gz +RUN tar -xzvf caddy_2.5.1_linux_amd64.tar.gz +RUN rm caddy_2.5.1_linux_amd64.tar.gz + +ENTRYPOINT ["sh", "-c", "/opt/heroku_startup.sh"] diff --git a/Procfile b/Procfile deleted file mode 100644 index cc34310..0000000 --- a/Procfile +++ /dev/null @@ -1,2 +0,0 @@ -web: python3 -m uvicorn weatherapi:app --reload --host=0.0.0.0 --port=${PORT:-5000} -bot: python3 bot.py \ No newline at end of file diff --git a/app.json b/app.json index c888ec5..2722075 100644 --- a/app.json +++ b/app.json @@ -3,18 +3,25 @@ "description": "Visual Weather api. Returns beautiful pictures with the current weather.", "repository": "https://github.com/mishailovic/VWapi", "website": "https://github.com/mishailovic/VWapi", + "stack": "container", "env": { + "APP_NAME": { + "description": "Heroku app name", + "value": "vwapi" + }, "BOT_TOKEN": { "description": "Your Telegram bot token", "value": "" }, "LRU_SIZE": { "description": "Size on an LRU cache, by default takes around 51 MB of RAM when full", - "value": "1024" + "value": "1024", + "required": false }, "LRU_EXPIRE": { "description": "Time in seconds after which LRU cache entry expires (default: 30 min)", - "value": "1800" + "value": "1800", + "required": false }, "MONGO_DB": { "description": "Telegram bot MongoDB database for analytics (disabled if not set)", @@ -23,7 +30,8 @@ }, "API_BASE": { "description": "Change it to your local instance for better telegram bot performance.", - "value": "https://vwapi.herokuapp.com" + "value": "https://vwapi.herokuapp.com", + "required": false } } } \ No newline at end of file diff --git a/bot.py b/bot.py index 2581af2..c709f87 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,6 @@ import urllib import uuid +import os from pymongo import MongoClient from aiogram import Bot, Dispatcher, executor, types @@ -118,4 +119,20 @@ async def inline_echo(inline_query: InlineQuery): if __name__ == "__main__": - executor.start_polling(dp, skip_updates=True) + # Heroku defines DYNO environment vaiable + if os.environ.get("DYNO"): + app_name = os.environ.get("APP_NAME") + + async def on_startup(dp): + await bot.set_webhook(f"https://{app_name}.herokuapp.com/bot/{TOKEN}") + + executor.start_webhook( + dispatcher=dp, + webhook_path=f"/bot/{TOKEN}", + on_startup=on_startup, + skip_updates=True, + host="localhost", + port=8081, # Caddy will take care of that + ) + else: + executor.start_polling(dp, skip_updates=True) diff --git a/config.py b/config.py index e105f8a..d935da1 100644 --- a/config.py +++ b/config.py @@ -2,11 +2,11 @@ OWM_TOKEN = "9de243494c0b295cca9337e1e96b00e2" # pro token already here, no need to change it -LRU_SIZE = 1024 # Size on an LRU cache, by default takes around 51 MB of RAM when full -LRU_EXPIRE = 1800 # Time in seconds after which LRU cache entry expires (default: 30 min) +LRU_SIZE = int(os.environ.get("LRU_SIZE", "1024")) # Size on an LRU cache, by default takes around 51 MB of RAM when full +LRU_EXPIRE = int(os.environ.get("LRU_EXPIRE", "1800")) # Time in seconds after which LRU cache entry expires (default: 30 min) -TOKEN = os.environ.get('BOT_TOKEN') # Telegram bot token, (TOKEN = "123456:qwertyuiop") if you running locally +TOKEN = os.environ.get("BOT_TOKEN") # Telegram bot token, (TOKEN = "123456:qwertyuiop") if you running locally -MONGO_DB = "" # Telegram bot MongoDB database for analytics (disabled if not set) +MONGO_DB = os.environ.get("MONGO_DB", "") # Telegram bot MongoDB database for analytics (disabled if not set) -API_BASE = "https://vwapi.herokuapp.com" # Change it to your local instance for better telegram bot performance. \ No newline at end of file +API_BASE = os.environ.get("API_BASE", "https://vwapi.herokuapp.com") # Change it to your local instance for better telegram bot performance. diff --git a/heroku.yml b/heroku.yml new file mode 100644 index 0000000..8eec25b --- /dev/null +++ b/heroku.yml @@ -0,0 +1,3 @@ +build: + docker: + web: Dockerfile diff --git a/heroku_startup.sh b/heroku_startup.sh new file mode 100755 index 0000000..a3f5815 --- /dev/null +++ b/heroku_startup.sh @@ -0,0 +1,17 @@ +# Start the services in the background +python3 -m uvicorn weatherapi:app --port=8080 & +python3 bot.py & + +cat << EOF > /opt/Caddyfile +http://:${PORT} { + route / { + reverse_proxy localhost:8080 + } + + route /bot/* { + reverse_proxy localhost:8081 + } +} +EOF + +/opt/caddy run -config /opt/Caddyfile