Skip to content

Commit

Permalink
Update with extracting routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanschultzie committed Jul 5, 2024
1 parent 265176b commit f96e188
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
5 changes: 5 additions & 0 deletions app/common.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
7 changes: 1 addition & 6 deletions app/ethereum.py → app/ethereum/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 3 additions & 30 deletions run.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down

0 comments on commit f96e188

Please sign in to comment.