generated from nathanwoodburn/python-webserver-template
Compare commits
2 Commits
206b323be6
...
b6662f400a
| Author | SHA1 | Date | |
|---|---|---|---|
|
b6662f400a
|
|||
|
1c51e97354
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ __pycache__/
|
||||
.env
|
||||
.vs/
|
||||
.venv/
|
||||
fireexplorer.db
|
||||
|
||||
@@ -22,6 +22,7 @@ RUN --mount=type=cache,target=/root/.cache/uv \
|
||||
|
||||
# Add mount point for data volume
|
||||
ENV BASE_DIR=/data
|
||||
ENV DATABASE_PATH=/data/fireexplorer.db
|
||||
VOLUME /data
|
||||
|
||||
EXPOSE 5000
|
||||
|
||||
77
server.py
77
server.py
@@ -5,9 +5,12 @@ from flask import (
|
||||
send_from_directory,
|
||||
send_file,
|
||||
jsonify,
|
||||
g,
|
||||
request,
|
||||
)
|
||||
import os
|
||||
import requests
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
import dotenv
|
||||
from tools import hip2, wallet_txt
|
||||
@@ -16,6 +19,40 @@ dotenv.load_dotenv()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
DATABASE = os.getenv("DATABASE_PATH", "fireexplorer.db")
|
||||
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
db = g._database = sqlite3.connect(DATABASE)
|
||||
db.row_factory = sqlite3.Row
|
||||
return db
|
||||
|
||||
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(g, "_database", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
|
||||
def init_db():
|
||||
with app.app_context():
|
||||
db = get_db()
|
||||
db.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS names (
|
||||
namehash TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
)
|
||||
"""
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
init_db()
|
||||
|
||||
|
||||
def find(name, path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
@@ -133,6 +170,32 @@ def catch_all(path: str):
|
||||
return render_template("404.html"), 404
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region API routes
|
||||
@app.route("/api/v1/namehash/<namehash>")
|
||||
def namehash_api(namehash):
|
||||
db = get_db()
|
||||
cur = db.execute("SELECT * FROM names WHERE namehash = ?", (namehash,))
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
# Get namehash from hsd.hns.au
|
||||
req = requests.get(f"https://hsd.hns.au/api/v1/namehash/{namehash}")
|
||||
if req.status_code == 200:
|
||||
name = req.json().get("result")
|
||||
if not name:
|
||||
return jsonify({"name": "Error", "namehash": namehash})
|
||||
# Insert into database
|
||||
db.execute(
|
||||
"INSERT OR REPLACE INTO names (namehash, name) VALUES (?, ?)",
|
||||
(namehash, name),
|
||||
)
|
||||
db.commit()
|
||||
return jsonify({"name": name, "namehash": namehash})
|
||||
return jsonify(dict(row))
|
||||
|
||||
|
||||
@app.route("/api/v1/status")
|
||||
def api_status():
|
||||
return jsonify(
|
||||
@@ -176,6 +239,18 @@ def hip02(domain: str):
|
||||
)
|
||||
|
||||
|
||||
@app.route("/api/v1/covenant", methods=["POST"])
|
||||
def covenant_api():
|
||||
data = request.get_json()
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"success": True,
|
||||
"data": data,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
@@ -188,4 +263,4 @@ def not_found(e):
|
||||
|
||||
# endregion
|
||||
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")
|
||||
|
||||
@@ -1178,18 +1178,24 @@
|
||||
}
|
||||
|
||||
showLoading('name-result');
|
||||
const data = await apiCall(`namehash/${nameHash}`);
|
||||
|
||||
// Check if result is valid and redirect to name page
|
||||
try {
|
||||
const response = await fetch(`/api/v1/namehash/${nameHash}`);
|
||||
const data = await response.json();
|
||||
|
||||
const resultElement = document.getElementById('name-result');
|
||||
if (data.error) {
|
||||
resultElement.innerHTML = `<div class="error">Error: ${data.error.message ? data.error.message : "Failed to lookup hash"}</div>`;
|
||||
} else if (data.result && typeof data.result === 'string') {
|
||||
resultElement.innerHTML = `<div class="error">Error: ${data.error}</div>`;
|
||||
} else if (data.name) {
|
||||
// Valid name found, redirect to name page
|
||||
window.location.href = `/name/${data.result}`;
|
||||
window.location.href = `/name/${data.name}`;
|
||||
} else {
|
||||
resultElement.innerHTML = `<div class="error">No name found for this hash</div>`;
|
||||
}
|
||||
} catch (e) {
|
||||
const resultElement = document.getElementById('name-result');
|
||||
resultElement.innerHTML = `<div class="error">Error: ${e.message}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
// Load status when page loads
|
||||
|
||||
Reference in New Issue
Block a user