Skip to main content

Framework-agnostic REST healthcheck endpoints for Python web apps

Project description

rest-health

rest-health logo

Framework-agnostic REST healthcheck endpoints for Python web apps

rest-health provides a simple, lightweight way to expose standardized REST healthcheck endpoints across different Python web frameworks. No unnecessary complexity - just clean, production-ready health monitoring.

Features

  • Framework-agnostic: Works with FastAPI, Flask, and any other Python web framework
  • Lightweight: Zero external dependencies for core functionality
  • Simple API: Register health checks as simple functions
  • Production-ready: Proper error handling and standard HTTP status codes
  • Extensible: Easy to add custom health checks

Installation

pip install rest-health

For framework-specific integrations:

# For FastAPI
pip install rest-health[fastapi]

# For Flask
pip install rest-health[flask]

Quick Start

Basic Usage

from rest_health import HealthCheck

# Create a health checker
health = HealthCheck()

# Add a simple check
def database_check():
    # Your database connectivity check logic
    return True  # or False if unhealthy

health.add_check("database", database_check)

# Run all checks
result = health.run()
# Returns: {"status": "ok", "checks": {"database": {"status": "ok", "success": True}}}

FastAPI Integration

from fastapi import FastAPI
from rest_health import HealthCheck
from rest_health.adapters.fastapi import create_fastapi_healthcheck

app = FastAPI()

# Setup health checks
health = HealthCheck()

def database_check():
    # Check database connectivity
    return True

def cache_check():
    # Check cache connectivity  
    return True

health.add_check("database", database_check)
health.add_check("cache", cache_check)

# Add health endpoint
health_router = create_fastapi_healthcheck(health)
app.include_router(health_router)

# Now GET /health returns health status

Flask Integration

from flask import Flask
from rest_health import HealthCheck
from rest_health.adapters.flask import create_flask_healthcheck

app = Flask(__name__)

# Setup health checks
health = HealthCheck()

def database_check():
    # Check database connectivity
    return True

health.add_check("database", database_check)

# Add health endpoint
health_blueprint = create_flask_healthcheck(health)
app.register_blueprint(health_blueprint)

# Now GET /health returns health status

Advanced Usage

Custom Health Checks

from rest_health import HealthCheck
import requests

health = HealthCheck()

def external_api_check():
    """Check if external API is responsive."""
    try:
        response = requests.get("https://api.example.com/ping", timeout=5)
        return response.status_code == 200
    except:
        return False

def database_check():
    """Check database connectivity."""
    try:
        # Your database check logic here
        return True
    except:
        return False

health.add_check("external_api", external_api_check)
health.add_check("database", database_check)

Error Handling

When a health check raises an exception, it's automatically caught and the check is marked as failed:

def failing_check():
    raise ValueError("Something went wrong")

health.add_check("problematic_service", failing_check)

result = health.run()
# Returns:
# {
#   "status": "fail",
#   "checks": {
#     "problematic_service": {
#       "status": "fail",
#       "success": False,
#       "error": "Something went wrong"
#     }
#   }
# }

Custom Endpoint Path

# FastAPI - custom path
health_router = create_fastapi_healthcheck(health, path="/api/health")

# Flask - custom path  
health_blueprint = create_flask_healthcheck(health, path="/api/health")

Response Format

Health check endpoints return JSON with the following structure:

{
  "status": "ok",  // "ok" or "fail"
  "checks": {
    "database": {
      "status": "ok",     // "ok" or "fail"
      "success": true
    },
    "cache": {
      "status": "fail",
      "success": false,
      "error": "Connection timeout"  // Only present on error
    }
  }
}

HTTP Status Codes

  • 200 OK: All health checks passed
  • 503 Service Unavailable: One or more health checks failed

Design Philosophy

rest-health follows these principles:

  • Simplicity: No background tasks, no async complexity, no unnecessary features
  • Framework-agnostic: Core logic works with any Python web framework
  • Lightweight: Minimal dependencies and overhead
  • Production-ready: Proper error handling and observability

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

For detailed technical information, development setup, coding standards, and contribution guidelines, please see our CONTRIBUTING.md file.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

rest_health-0.1.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

rest_health-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file rest_health-0.1.1.tar.gz.

File metadata

  • Download URL: rest_health-0.1.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rest_health-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dd29944034c9d8e7bb304a4a79c8a0054e7cfcc8e366f2fa98a5d26d61fc7882
MD5 dec2440166d87f2de884d4c53180a02d
BLAKE2b-256 eecafc23cc189688e9cab3b1fe28e871445d50f6d8fe7221a2a4cc74cc36d5a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for rest_health-0.1.1.tar.gz:

Publisher: publish.yml on fabricio-entringer/rest-health

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rest_health-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rest_health-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rest_health-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b4ecaa88d3346f958ccd0dcd0bc73dc1e246ae785d3e06ae07a8eae3cbdc51b
MD5 c8c6bffbe25cd1f94242a75022558cf2
BLAKE2b-256 a2d3821567706155e9b4882dc8f54a3e8cd62cc32973aecd5485e5a3982d6dd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for rest_health-0.1.1-py3-none-any.whl:

Publisher: publish.yml on fabricio-entringer/rest-health

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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