diff --git a/app/common.py b/app/common.py new file mode 100644 index 0000000..552d787 --- /dev/null +++ b/app/common.py @@ -0,0 +1,5 @@ +def parse_request(req) -> tuple: + port = req.headers.get("Port") + host = req.headers.get("Host") + ip_address = host.split(":")[0] + return ip_address, port diff --git a/app/cosmos.py b/app/cosmos/cosmos.py similarity index 100% rename from app/cosmos.py rename to app/cosmos/cosmos.py diff --git a/app/ethereum.py b/app/ethereum/ethereum.py similarity index 87% rename from app/ethereum.py rename to app/ethereum/ethereum.py index 6d78981..a3bf656 100644 --- a/app/ethereum.py +++ b/app/ethereum/ethereum.py @@ -6,12 +6,7 @@ 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", - "method": "eth_getBlockByNumber", - "params": ["latest", False], - "id": 1 - } + payload = {"jsonrpc": "2.0", "method": "eth_getBlockByNumber", "params": ["latest", False], "id": 1} headers = {"Content-Type": "application/json"} url = f"http://{ip}:{port}" response = requests.post(url, headers=headers, json=payload) diff --git a/app/routes.py b/app/routes.py new file mode 100644 index 0000000..be89d8e --- /dev/null +++ b/app/routes.py @@ -0,0 +1,21 @@ +# routes.py +from flask import Blueprint, request +from app.common import parse_request +from app.cosmos.cosmos import cosmos_health +from app.ethereum.ethereum import ethereum_health + +api = Blueprint("api", __name__) + + +@api.route("/ethereum", methods=["GET"]) +def ethereum_health_check(): + ip, port = parse_request(request) + message, status = ethereum_health(ip, port) + return message, status + + +@api.route("/cosmos", methods=["GET"]) +def cosmos_health_check(): + ip, port = parse_request(request) + message, status = cosmos_health(ip, port) + return message, status diff --git a/run.py b/run.py index 7fd3ef2..9f24321 100644 --- a/run.py +++ b/run.py @@ -1,35 +1,8 @@ -from dotenv import load_dotenv -from flask import Flask, request -from app.ethereum import ethereum_health -from app.cosmos import cosmos_health - -load_dotenv() +from flask import Flask +from app.routes import api # Import the Blueprint app = Flask(__name__) - -# 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 +app.register_blueprint(api) # Register the Blueprint def main():