From bfc1f0839a47ddeb07636f649a21479921be3e89 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Sat, 11 Oct 2025 18:51:22 +1100 Subject: [PATCH] feat: Move getGitCommit from api to tools --- blueprints/api.py | 22 +--------------------- server.py | 4 ++-- tools.py | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/blueprints/api.py b/blueprints/api.py index cafc8d9..3ca2497 100644 --- a/blueprints/api.py +++ b/blueprints/api.py @@ -4,7 +4,7 @@ import datetime import requests from mail import sendEmail from sol import create_transaction -from tools import getClientIP +from tools import getClientIP, getGitCommit api_bp = Blueprint('api', __name__) @@ -18,26 +18,6 @@ if 'time-zone' not in ncConfig: ncConfig['time-zone'] = 10 -def getGitCommit(): - # if .git exists, get the latest commit hash - if os.path.isdir(".git"): - git_dir = ".git" - head_ref = "" - with open(os.path.join(git_dir, "HEAD")) as file: - head_ref = file.read().strip() - if head_ref.startswith("ref: "): - head_ref = head_ref[5:] - with open(os.path.join(git_dir, head_ref)) as file: - return file.read().strip() - else: - return head_ref - - # Check if env SOURCE_COMMIT is set - if "SOURCE_COMMIT" in os.environ: - return os.environ["SOURCE_COMMIT"] - - return "failed to get version" - @api_bp.route("/") @api_bp.route("/help") def help_get(): diff --git a/server.py b/server.py index b33dcf3..e0e5727 100644 --- a/server.py +++ b/server.py @@ -22,10 +22,10 @@ from PIL import Image from blueprints.now import now_bp from blueprints.blog import blog_bp from blueprints.wellknown import wk_bp -from blueprints.api import api_bp, getGitCommit +from blueprints.api import api_bp from blueprints.podcast import podcast_bp from blueprints.acme import acme_bp -from tools import isCurl, isCrawler, getAddress, getFilePath, error_response, getClientIP, json_response +from tools import isCurl, isCrawler, getAddress, getFilePath, error_response, getClientIP, json_response, getGitCommit app = Flask(__name__) CORS(app) diff --git a/tools.py b/tools.py index 84b301f..aae6e93 100644 --- a/tools.py +++ b/tools.py @@ -10,6 +10,27 @@ def getClientIP(request): ip = request.remote_addr return ip +def getGitCommit(): + # if .git exists, get the latest commit hash + if os.path.isdir(".git"): + git_dir = ".git" + head_ref = "" + with open(os.path.join(git_dir, "HEAD")) as file: + head_ref = file.read().strip() + if head_ref.startswith("ref: "): + head_ref = head_ref[5:] + with open(os.path.join(git_dir, head_ref)) as file: + return file.read().strip() + else: + return head_ref + + # Check if env SOURCE_COMMIT is set + if "SOURCE_COMMIT" in os.environ: + return os.environ["SOURCE_COMMIT"] + + return "failed to get version" + + def isCurl(request: Request) -> bool: """ Check if the request is from curl