From f96e1884442000cd71c359a5b8243bd322806081 Mon Sep 17 00:00:00 2001 From: Dylan Schultz <9121234+dylanschultzie@users.noreply.github.com> Date: Fri, 5 Jul 2024 15:38:13 -0700 Subject: [PATCH] Update with extracting routes --- app/common.py | 5 +++++ app/{ => cosmos}/cosmos.py | 0 app/{ => ethereum}/ethereum.py | 7 +------ app/routes.py | 21 +++++++++++++++++++++ run.py | 33 +++------------------------------ 5 files changed, 30 insertions(+), 36 deletions(-) create mode 100644 app/common.py rename app/{ => cosmos}/cosmos.py (100%) rename app/{ => ethereum}/ethereum.py (87%) create mode 100644 app/routes.py 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():