For anyone who is not a coder (like me). I just used ChatGPT to create an API endpoint for the my.hc1hode.com. Here is the code that I iteratively produced. It took me about 25 revisions. Each revision I added and tested the code.
This code is designed to respond to a healthcheck query on the endpoint :8080/healthcheck
It returns an HTTP status along with json message to the browser. Then it logs error messages and keeps track of the status with a simple txt file. Then it sends a notification to Telegram of status change in a threaded process so as to not slow down the healthcheck response to the load balancer.
I did this in python because Iām most familiar with it. Anyone here, any non-dev can use chatGPT to help read the go-zenon code. Anyone can contribute to the code base with the help of chatGPT. Just gotta try!
from flask import Flask, jsonify
import requests
import json
import logging
import telegram_send
import threading
app = Flask(__name__)
# Set up logging
logging.basicConfig(level=logging.INFO)
# The filename of the text file where we'll store the last status code
status_file = "healthstatus.txt"
# Define node name
node_name = "Node-02"
def read_last_status_code():
try:
with open(status_file, 'r') as f:
return f.read().strip()
except Exception:
return '200 OK' # If we can't read the file for any reason, assume the last status was 200 OK
def write_last_status_code(code):
with open(status_file, 'w') as f:
f.write(str(code))
def send_telegram_message(message):
telegram_send.send(messages=[message])
@app.route("/healthcheck", methods=['GET'])
def healthcheck():
url = "https://my.hc1node.com:35997"
headers = {"content-type": "application/json"}
data = {"jsonrpc": "2.0", "id": 21, "method": "stats.syncInfo", "params": []}
timeout = 5 # in seconds
try:
response = requests.get(url, headers=headers, data=json.dumps(data), timeout=timeout)
if response.status_code == 200:
json_response = response.json()
if "result" in json_response and "state" in json_response["result"] and json_response["result"]["state"] in [2, 3]:
state = json_response["result"]["state"]
logging.info(f"Health check OK, state: {state}")
# If the last status code was not 200, send a message to Telegram
if read_last_status_code() != '200 OK':
threading.Thread(target=send_telegram_message, args=(f"{node_name}: Healthcheck is now OK",)).start()
write_last_status_code('200 OK')
return jsonify({"message": "OK"}), 200
except requests.Timeout as e:
logging.error(f"Request timeout: {e}")
if read_last_status_code() != '504 Timeout Error':
threading.Thread(target=send_telegram_message, args=(f"{node_name}: Healthcheck resulted in Timeout Error",)).start()
write_last_status_code('504 Timeout Error')
return jsonify({"message": "Timeout Error"}), 504
except requests.ConnectionError as e:
logging.error(f"Connection error: {e}")
if read_last_status_code() != '503 Connection Error':
threading.Thread(target=send_telegram_message, args=(f"{node_name}: Healthcheck resulted in Connection Error",)).start()
write_last_status_code('503 Connection Error')
return jsonify({"message": "Connection Error"}), 503
if read_last_status_code() != '503 Bad Gateway':
logging.warning("Bad Gateway")
threading.Thread(target=send_telegram_message, args=(f"{node_name}: Healthcheck resulted in Bad Gateway",)).start()
write_last_status_code('503 Bad Gateway')
return jsonify({"message": "Bad Gateway"}), 503
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)