diff --git a/app/cosmos.py b/app/cosmos.py index 8d74c0c..4ac51f6 100644 --- a/app/cosmos.py +++ b/app/cosmos.py @@ -1,6 +1,7 @@ import requests from datetime import datetime, timedelta, timezone + def cosmos_health(ip: str, port: str, acceptable_time_delta: int = 60) -> tuple: acceptable_time_delta = timedelta(seconds=acceptable_time_delta) @@ -18,7 +19,7 @@ def cosmos_health(ip: str, port: str, acceptable_time_delta: int = 60) -> tuple: current_dt = datetime.now(timezone.utc) latest_block_time = latest_block_data["result"]["sync_info"]["latest_block_time"] - truncated_time_str = latest_block_time[:-4] + 'Z' + truncated_time_str = latest_block_time[:-4] + "Z" latest_block_dt = datetime.strptime(truncated_time_str, "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc) block_time_delta = current_dt - latest_block_dt diff --git a/app/ethereum.py b/app/ethereum.py index 31b6344..6d78981 100644 --- a/app/ethereum.py +++ b/app/ethereum.py @@ -1,9 +1,10 @@ from datetime import datetime, timedelta, timezone import requests + def ethereum_health(ip: str, port: str, acceptable_time_delta: int = 60): acceptable_time_delta = timedelta(seconds=acceptable_time_delta) - + # Get latest block data payload = { "jsonrpc": "2.0", diff --git a/run.py b/run.py index d17e585..7fd3ef2 100644 --- a/run.py +++ b/run.py @@ -1,5 +1,3 @@ -import os - from dotenv import load_dotenv from flask import Flask, request from app.ethereum import ethereum_health @@ -12,25 +10,30 @@ # Time in seconds between current time and last block ACCEPTABLE_DELTA = 60 + @app.route("/ethereum", methods=["GET"]) def ethereum_health_check(): ip, port = parse_request(request) message, status = ethereum_health(ip, port) return message, status + @app.route("/cosmos", methods=["GET"]) def health_check(): ip, port = parse_request(request) message, status = cosmos_health(ip, port) return message, status + def parse_request(req) -> tuple: port = req.headers.get("Port") host = req.headers.get("Host") ip_address = host.split(":")[0] return ip_address, port + def main(): app.run(host="0.0.0.0", port=53336) + main()