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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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