Skip to content

Commit

Permalink
Redo Heroku stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
notmyst33d committed May 11, 2022
1 parent 94e5fce commit fd15ba8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 0 additions & 2 deletions Procfile

This file was deleted.

14 changes: 11 additions & 3 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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
}
}
}
19 changes: 18 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import urllib
import uuid
import os

from pymongo import MongoClient
from aiogram import Bot, Dispatcher, executor, types
Expand Down Expand Up @@ -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)
10 changes: 5 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
API_BASE = os.environ.get("API_BASE", "https://vwapi.herokuapp.com") # Change it to your local instance for better telegram bot performance.
3 changes: 3 additions & 0 deletions heroku.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
docker:
web: Dockerfile
17 changes: 17 additions & 0 deletions heroku_startup.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fd15ba8

Please sign in to comment.