generated from nathanwoodburn/python-webserver-template
feat: Add initial code
All checks were successful
Build Docker / BuildImage (push) Successful in 51s
All checks were successful
Build Docker / BuildImage (push) Successful in 51s
This commit is contained in:
2
example.env
Normal file
2
example.env
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
HSD_HOST=127.0.0.1
|
||||||
|
HSD_API_KEY=APIKEY
|
||||||
258
server.py
258
server.py
@@ -18,6 +18,12 @@ import dotenv
|
|||||||
|
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
|
HSD_HOST = os.getenv("HSD_HOST", "127.0.0.1")
|
||||||
|
HSD_PORT = os.getenv("HSD_PORT", "12037")
|
||||||
|
HSD_API_KEY = os.getenv("HSD_API_KEY", "y5cSK42tgVCdt4E58jkHjI3nQ9GU32bC")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -26,6 +32,15 @@ def find(name, path):
|
|||||||
if name in files:
|
if name in files:
|
||||||
return os.path.join(root, name)
|
return os.path.join(root, name)
|
||||||
|
|
||||||
|
|
||||||
|
def HSD_URL():
|
||||||
|
"""
|
||||||
|
Returns the HSD URL based on the environment variables.
|
||||||
|
"""
|
||||||
|
if not HSD_API_KEY:
|
||||||
|
return f"http://{HSD_HOST}:{HSD_PORT}"
|
||||||
|
return f"http://x:{HSD_API_KEY}@{HSD_HOST}:{HSD_PORT}"
|
||||||
|
|
||||||
# Assets routes
|
# Assets routes
|
||||||
@app.route("/assets/<path:path>")
|
@app.route("/assets/<path:path>")
|
||||||
def send_assets(path):
|
def send_assets(path):
|
||||||
@@ -106,25 +121,242 @@ def catch_all(path: str):
|
|||||||
|
|
||||||
# region API routes
|
# region API routes
|
||||||
|
|
||||||
api_requests = 0
|
@app.route("/api/v1/status")
|
||||||
|
def api_status():
|
||||||
@app.route("/api/v1/data", methods=["GET"])
|
|
||||||
def api_data():
|
|
||||||
"""
|
"""
|
||||||
Example API endpoint that returns some data.
|
This endpoint checks the status of the HSD node.
|
||||||
You can modify this to return whatever data you need.
|
"""
|
||||||
|
try:
|
||||||
|
response = requests.get(HSD_URL())
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify({"status": "HSD is running"}), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "HSD is not running"}), 503
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/chain")
|
||||||
|
def api_chain():
|
||||||
|
"""
|
||||||
|
This endpoint returns the chain status of the HSD node.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = requests.get(HSD_URL())
|
||||||
|
if response.status_code == 200:
|
||||||
|
if 'chain' in response.json():
|
||||||
|
chain_status = response.json()['chain']
|
||||||
|
return jsonify({"chain": chain_status}), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Chain status not found in response"}), 503
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get chain status"}), 503
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/mempool")
|
||||||
|
def mempool():
|
||||||
|
"""
|
||||||
|
This endpoint returns the current mempool status from the HSD node.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
global api_requests
|
try:
|
||||||
api_requests += 1
|
url = f"{HSD_URL()}/mempool"
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(response.json()), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Command failed", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/<datatype>/<blockid>")
|
||||||
|
def api_block_or_header(datatype, blockid):
|
||||||
|
"""
|
||||||
|
This endpoint returns block or header data for the given blockid from the HSD node.
|
||||||
|
Allowed datatypes: 'block', 'header'
|
||||||
|
"""
|
||||||
|
if datatype not in ["block", "header"]:
|
||||||
|
# Return a 404 error for invalid datatype
|
||||||
|
return jsonify({"error": "API endpoint not found"}), 400
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/{datatype}/{blockid}"
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(response.json()), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": f"Failed to get {datatype}", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/coin/<coinhash>/<index>")
|
||||||
|
def api_coin(coinhash, index):
|
||||||
|
"""
|
||||||
|
This endpoint returns information about a specific coin.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/coin/{coinhash}/{index}"
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(response.json()), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get coin data", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/coin/address/<address>")
|
||||||
|
def api_coin_address(address):
|
||||||
|
"""
|
||||||
|
This endpoint returns information about coins for a specific address.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/coin/address/{address}"
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(response.json()), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get coins for address", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/tx/<txid>")
|
||||||
|
def api_transaction(txid):
|
||||||
|
"""
|
||||||
|
This endpoint returns information about a specific transaction.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/tx/{txid}"
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(response.json()), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get transaction data", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/tx/address/<address>")
|
||||||
|
def api_transaction_address(address):
|
||||||
|
"""
|
||||||
|
This endpoint returns transactions for a specific address.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/tx/address/{address}"
|
||||||
|
response = requests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(response.json()), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get transactions for address", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/name/<name>")
|
||||||
|
def api_name(name):
|
||||||
|
"""
|
||||||
|
This endpoint returns information about a specific name.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/"
|
||||||
data = {
|
data = {
|
||||||
"header": "Sample API Response",
|
"method": "getnameinfo",
|
||||||
"content": f"Hello, this is a sample API response! You have called this endpoint {api_requests} times.",
|
"params": [name]
|
||||||
"timestamp": datetime.now().isoformat(),
|
|
||||||
}
|
}
|
||||||
return jsonify(data)
|
response = requests.post(url, json=data)
|
||||||
|
if response.status_code == 200:
|
||||||
|
# Check if error is null
|
||||||
|
if 'error' in response.json() and response.json()['error'] is not None:
|
||||||
|
return jsonify({"error": response.json()['error']}), 400
|
||||||
|
|
||||||
|
# Check if result is empty
|
||||||
|
if 'result' not in response.json() or not response.json()['result']:
|
||||||
|
return jsonify({"error": "Name not found"}), 404
|
||||||
|
return jsonify(response.json()['result']), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get name data", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/namehash/<namehash>")
|
||||||
|
def api_namehash(namehash):
|
||||||
|
"""
|
||||||
|
This endpoint returns information about a specific name.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/"
|
||||||
|
data = {
|
||||||
|
"method": "getnamebyhash",
|
||||||
|
"params": [namehash]
|
||||||
|
}
|
||||||
|
response = requests.post(url, json=data)
|
||||||
|
if response.status_code == 200:
|
||||||
|
# Check if error is null
|
||||||
|
if 'error' in response.json() and response.json()['error'] is not None:
|
||||||
|
return jsonify({"error": response.json()['error']}), 400
|
||||||
|
|
||||||
|
# Check if result is empty
|
||||||
|
if 'result' not in response.json() or not response.json()['result']:
|
||||||
|
return jsonify({"error": "Name not found"}), 404
|
||||||
|
return jsonify(response.json()['result']), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get name data", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route("/api/v1/nameresource/<name>")
|
||||||
|
def api_nameresource(name):
|
||||||
|
"""
|
||||||
|
This endpoint returns the resource for a specific name.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
url = f"{HSD_URL()}/"
|
||||||
|
data = {
|
||||||
|
"method": "getnameresource",
|
||||||
|
"params": [name]
|
||||||
|
}
|
||||||
|
response = requests.post(url, json=data)
|
||||||
|
if response.status_code == 200:
|
||||||
|
# Check if error is null
|
||||||
|
if 'error' in response.json() and response.json()['error'] is not None:
|
||||||
|
return jsonify({"error": response.json()['error']}), 400
|
||||||
|
|
||||||
|
# Check if result is empty
|
||||||
|
if 'result' not in response.json() or not response.json()['result']:
|
||||||
|
return jsonify({"error": "Resource not found"}), 404
|
||||||
|
return jsonify(response.json()['result']), 200
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Failed to get name resource", "status_code": response.status_code}), response.status_code
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/v1/help")
|
||||||
|
def api_help():
|
||||||
|
"""
|
||||||
|
This endpoint returns a list of available API endpoints and their descriptions.
|
||||||
|
"""
|
||||||
|
api_endpoints = [
|
||||||
|
{"endpoint": "/api/v1/status", "description": "Check HSD node status"},
|
||||||
|
{"endpoint": "/api/v1/chain", "description": "Get chain status"},
|
||||||
|
{"endpoint": "/api/v1/mempool", "description": "Get mempool status"},
|
||||||
|
{"endpoint": "/api/v1/block/<blockid>", "description": "Get block data by block height or hash"},
|
||||||
|
{"endpoint": "/api/v1/header/<blockid>", "description": "Get header data by block height or hash"},
|
||||||
|
{"endpoint": "/api/v1/coin/<coinhash>/<index>", "description": "Get coin info"},
|
||||||
|
{"endpoint": "/api/v1/coin/address/<address>", "description": "Get coins for address"},
|
||||||
|
{"endpoint": "/api/v1/tx/<txid>", "description": "Get transaction info"},
|
||||||
|
{"endpoint": "/api/v1/tx/address/<address>", "description": "Get transactions for address"},
|
||||||
|
{"endpoint": "/api/v1/name/<name>", "description": "Get name info"},
|
||||||
|
{"endpoint": "/api/v1/namehash/<namehash>", "description": "Get name by hash"},
|
||||||
|
{"endpoint": "/api/v1/nameresource/<name>", "description": "Get name resource"},
|
||||||
|
{"endpoint": "/api/v1/help", "description": "List all API endpoints"},
|
||||||
|
]
|
||||||
|
return jsonify({"api": api_endpoints}), 200
|
||||||
|
|
||||||
|
@app.route("/api/v1")
|
||||||
|
@app.route("/api/v1/")
|
||||||
|
@app.route("/api/v1/<catch_all>")
|
||||||
|
def api_index(catch_all=None):
|
||||||
|
"""
|
||||||
|
This endpoint returns a 404 error for any API endpoint that is not found.
|
||||||
|
"""
|
||||||
|
return jsonify({"error": "API endpoint not found"}), 404
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
@@ -138,4 +370,4 @@ def not_found(e):
|
|||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True, port=5000, host="0.0.0.0")
|
app.run(debug=True, port=5000, host="127.0.0.1")
|
||||||
|
|||||||
@@ -18,24 +18,6 @@ a {
|
|||||||
a:hover {
|
a:hover {
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
|
.spacer {
|
||||||
/* Mike section styling */
|
height: 20px; /* Adjust as needed */
|
||||||
.mike-section {
|
|
||||||
margin-top: 30px;
|
|
||||||
max-width: 600px;
|
|
||||||
margin-left: auto;
|
|
||||||
margin-right: auto;
|
|
||||||
padding: 20px;
|
|
||||||
background-color: rgba(50, 50, 50, 0.3);
|
|
||||||
border-radius: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.mike-section h2 {
|
|
||||||
color: #f0f0f0;
|
|
||||||
margin-top: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.mike-section p {
|
|
||||||
line-height: 1.6;
|
|
||||||
margin-bottom: 15px;
|
|
||||||
}
|
}
|
||||||
@@ -4,57 +4,130 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Nathan.Woodburn/</title>
|
<title>Fire HSD | Nathan.Woodburn</title>
|
||||||
<link rel="icon" href="/assets/img/favicon.png" type="image/png">
|
<link rel="icon" href="/assets/img/favicon.png" type="image/png">
|
||||||
<link rel="stylesheet" href="/assets/css/index.css">
|
<link rel="stylesheet" href="/assets/css/index.css">
|
||||||
|
<style>
|
||||||
|
/* Extra styling for index page */
|
||||||
|
.intro {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto 2em auto;
|
||||||
|
padding: 1.5em;
|
||||||
|
background: rgba(40, 40, 40, 0.5);
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
|
||||||
|
font-size: 1.15em;
|
||||||
|
line-height: 1.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.api-table-container {
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
margin-top: 4em;
|
||||||
|
text-align: center;
|
||||||
|
color: #aaa;
|
||||||
|
font-size: 0.95em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo {
|
||||||
|
width: 80px;
|
||||||
|
height: 80px;
|
||||||
|
margin-bottom: 1em;
|
||||||
|
border-radius: 50%;
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="spacer"></div>
|
<div class="spacer"></div>
|
||||||
<div class="centre">
|
<div class="centre">
|
||||||
<h1>Nathan.Woodburn/</h1>
|
<img src="/assets/img/favicon.png" alt="Fire HSD Logo" class="logo">
|
||||||
<span>The current date and time is {{datetime}}</span>
|
<h1>Fire HSD</h1>
|
||||||
|
<span style="font-size:1.2em; color:#f0f0f0;">A free public API for Handshake (HSD)</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="spacer"></div>
|
<div class="spacer"></div>
|
||||||
<div class="centre">
|
<div class="intro">
|
||||||
<h2 id="test-content-header">Pulling data</h2>
|
Fire HSD is a public API for the <a href="https://handshake.org/" target="_blank">Handshake</a> blockchain.<br>
|
||||||
<span class="test-content">This is a test content area that will be updated with data from the server.</span>
|
You can use these endpoints to query blocks, transactions, coins and names.<br>
|
||||||
|
No authentication required!<br>
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<b>Quick Start:</b> Try <code>/api/v1/status</code> or <code>/api/v1/block/1</code> in your browser or with
|
||||||
<span class="test-content-timestamp">Timestamp: Waiting to pull data</span>
|
<code>curl</code>.
|
||||||
</div>
|
</div>
|
||||||
|
<div class="spacer"></div>
|
||||||
|
<div class="centre api-table-container">
|
||||||
|
<h2>API Endpoints</h2>
|
||||||
|
<div id="api-endpoints"></div>
|
||||||
|
</div>
|
||||||
|
<div class="spacer"></div>
|
||||||
|
<footer>
|
||||||
|
© 2025 Nathan Woodburn — <a href="https://git.woodburn.au/nathanwoodburn/firehsd" style="color:#aaa;">Source</a>
|
||||||
|
</footer>
|
||||||
<script>
|
<script>
|
||||||
function fetchData() {
|
function renderApiEndpoints(apiList) {
|
||||||
// Fetch the data from the server
|
const container = document.getElementById('api-endpoints');
|
||||||
fetch('/api/v1/data')
|
// Create table element
|
||||||
.then(response => response.json())
|
const table = document.createElement('table');
|
||||||
.then(data => {
|
table.style.margin = 'auto';
|
||||||
// Get the data header element
|
table.style.borderCollapse = 'collapse';
|
||||||
const dataHeader = document.getElementById('test-content-header');
|
table.style.width = '100%';
|
||||||
// Update the header with the fetched data
|
|
||||||
dataHeader.textContent = data.header;
|
|
||||||
// Get the test content element
|
|
||||||
const testContent = document.querySelector('.test-content');
|
|
||||||
// Update the content with the fetched data
|
|
||||||
testContent.textContent = data.content;
|
|
||||||
|
|
||||||
// Get the timestamp element
|
// Create table header
|
||||||
const timestampElement = document.querySelector('.test-content-timestamp');
|
const thead = document.createElement('thead');
|
||||||
// Update the timestamp with the fetched data
|
const headerRow = document.createElement('tr');
|
||||||
timestampElement.textContent = `Timestamp: ${data.timestamp}`;
|
const thEndpoint = document.createElement('th');
|
||||||
})
|
thEndpoint.style.padding = '8px';
|
||||||
.catch(error => console.error('Error fetching data:', error));
|
thEndpoint.style.borderBottom = '1px solid #ccc';
|
||||||
|
thEndpoint.textContent = 'Endpoint';
|
||||||
|
const thDescription = document.createElement('th');
|
||||||
|
thDescription.style.padding = '8px';
|
||||||
|
thDescription.style.borderBottom = '1px solid #ccc';
|
||||||
|
thDescription.textContent = 'Description';
|
||||||
|
headerRow.appendChild(thEndpoint);
|
||||||
|
headerRow.appendChild(thDescription);
|
||||||
|
thead.appendChild(headerRow);
|
||||||
|
table.appendChild(thead);
|
||||||
|
|
||||||
|
// Create table body
|
||||||
|
const tbody = document.createElement('tbody');
|
||||||
|
apiList.forEach(api => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
|
const tdEndpoint = document.createElement('td');
|
||||||
|
tdEndpoint.style.padding = '8px';
|
||||||
|
tdEndpoint.style.borderBottom = '1px solid #eee';
|
||||||
|
const code = document.createElement('code');
|
||||||
|
code.textContent = api.endpoint;
|
||||||
|
tdEndpoint.appendChild(code);
|
||||||
|
|
||||||
|
const tdDescription = document.createElement('td');
|
||||||
|
tdDescription.style.padding = '8px';
|
||||||
|
tdDescription.style.borderBottom = '1px solid #eee';
|
||||||
|
tdDescription.textContent = api.description;
|
||||||
|
|
||||||
|
row.appendChild(tdEndpoint);
|
||||||
|
row.appendChild(tdDescription);
|
||||||
|
tbody.appendChild(row);
|
||||||
|
});
|
||||||
|
table.appendChild(tbody);
|
||||||
|
|
||||||
|
// Replace container content
|
||||||
|
container.innerHTML = '';
|
||||||
|
container.appendChild(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial fetch after 2 seconds
|
fetch('/api/v1/help')
|
||||||
setTimeout(fetchData, 2000);
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
// Then fetch every 2 seconds
|
if (data.api) {
|
||||||
setInterval(fetchData, 2000);
|
renderApiEndpoints(data.api);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error fetching API endpoints:', error));
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user