Skip to main content

A simple healthcheck using aiohttp

Project description

aiohttp-healthcheck

A minimalist healthcheck library using aiohttp.

Installation

Install the package using pip:

pip install aio-healthcheck-python

Usage

Here is a basic example of how to use the aio-healthcheck-python library:

from aiohttp import web
from aio_healthcheck_python import start_healthcheck


async def check_database():
    # Implement your actual database check here.
    return True


async def handle_main_page(request):
    return web.Response(text="Hello, world")


async def start_web_server():
    app = web.Application()
    # Define routes here
    app.router.add_get('/', handle_main_page)

    # Set up and start the web server
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, '127.0.0.1', 8080)
    await site.start()

    print("Web server started on http://127.0.0.1:8080")
    return runner


async def main():
    # Start the web server and print its status
    web_runner = await start_web_server()

    # Start the healthcheck server with database check and print its status
    health_runner = await start_healthcheck(
        async_callables=[check_database],  # Asynchronous checks, including database
        host="127.0.0.1",
        path="/healthcheck",
        port=8000,
        success_code=200,
        error_code=500,
    )

    print("Healthcheck server started on http://127.0.0.1:8000")

    # Return the runners for potential further management
    return web_runner, health_runner


# Start the aiohttp application with the main coroutine
if __name__ == '__main__':
    web.run_app(main())

In this example, we define an asynchronous function check_database that performs a health check (in this case, it always returns True). We then start the healthcheck server with this function as an asynchronous callable. The server will respond with a 200 status code if the check passes, and a 500 status code if it fails.

Testing

To run the tests, use the following command:

PYTHONPATH=aio_healthcheck_python poetry run python -m unittest discover -s tests

License

This project is licensed under the terms of the Apache-2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio_healthcheck_python-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aio_healthcheck_python-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file aio_healthcheck_python-0.1.0.tar.gz.

File metadata

  • Download URL: aio_healthcheck_python-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.18 Linux/6.5.0-1016-azure

File hashes

Hashes for aio_healthcheck_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7aa481979bb3cfbde9af8d44256b84123ef6f0bc633f3302a5cd052f2cf6c417
MD5 fc5da92b586f4a7ba8397065de4319c8
BLAKE2b-256 cf2fe1fad73f24d7c58be26f428715319caabbb19341eaf3404cf3770194ef41

See more details on using hashes here.

File details

Details for the file aio_healthcheck_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aio_healthcheck_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f469a82595c4ed434fc1fa3bf6e59624b87d732ed7fcc1a672c8a1303920717d
MD5 727df415541bbfdd6425781f408baa0b
BLAKE2b-256 93cec002272572e2035df8c43890f511e6fa761ce6d1bf1d4bb4982405c05409

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page