Skip to content

Commit

Permalink
feat: add healthcheck endpoint (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
toolen committed Feb 7, 2022
1 parent 69f69e7 commit d24a0b3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

# Allow files and directories
!/webhook_telegram_bot
!/healthcheck.py
!/poetry.lock
!/pyproject.toml
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ RUN set -ex \

COPY --chown=app:app ./webhook_telegram_bot /app/webhook_telegram_bot

COPY --chown=app:app ./healthcheck.py /app

EXPOSE 8080

HEALTHCHECK --interval=10s --timeout=10s --retries=3 CMD /app/venv/bin/python healthcheck.py || exit 1

CMD [ "/sbin/tini", "--", "/app/venv/bin/python", "-m", "webhook_telegram_bot.main", "--host", "0.0.0.0", "--port", "8080"]
27 changes: 27 additions & 0 deletions healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
"""Custom healthcheck script."""
import sys
from urllib.error import HTTPError, URLError
from urllib.request import urlopen


def main() -> None:
"""
Entrypoint.
:return: None
"""
try:
response = urlopen('http://127.0.0.1:8080/api/v1/health') # nosec
if response.code == 200:
sys.exit(0)
else:
sys.exit(1)
except HTTPError:
sys.exit(1)
except URLError:
sys.exit(1)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "webhook-telegram-bot"
version = "1.0.0"
version = "1.1.0"
description = ""
authors = ["Dmitrii Zakharov <dmitrii@zakharov.cc>"]
license = "MIT"
Expand Down
16 changes: 16 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from aiohttp import web

from webhook_telegram_bot.main import init_routes


async def test_health_handler(aiohttp_client):
app = web.Application()
init_routes(app)
client = await aiohttp_client(app)
url = client.app.router["health"].url_for()
result = await client.get(url)

assert result.status == 200

data = await result.json()
assert data.get("health") == "ok"
12 changes: 12 additions & 0 deletions webhook_telegram_bot/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""This file contains handlers for common purposes."""
from aiohttp import web
from aiohttp.web_response import Response


async def health_handler(request: web.Request) -> Response:
"""
Return healthcheck response.
:return: Response
"""
return web.json_response({"health": "ok"})
12 changes: 12 additions & 0 deletions webhook_telegram_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ImproperlyConfiguredException,
WebhookBotException,
)
from webhook_telegram_bot.handlers import health_handler
from webhook_telegram_bot.helpers import (
get_config_value,
get_database,
Expand Down Expand Up @@ -175,6 +176,16 @@ async def on_shutdown_telegram_handler(app_: web.Application) -> None:
init_telegram_routes(app)


def init_routes(app: web.Application) -> None:
"""
Initialize common application routes.
:param app: application instance
:return:
"""
app.router.add_route("GET", "/api/v1/health", health_handler, name="health")


async def create_app(config: Optional[Dict[str, str]] = None) -> web.Application:
"""
Create application.
Expand All @@ -189,6 +200,7 @@ async def create_app(config: Optional[Dict[str, str]] = None) -> web.Application
init_plugins(app)
init_templates(app)
init_telegram(app)
init_routes(app)
return app


Expand Down

0 comments on commit d24a0b3

Please sign in to comment.