From ed845a3d9aefef2950fe877746f5998ad052684c Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Fri, 14 Nov 2025 22:50:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=8E=20pyproject.toml=20=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E9=A1=B9=E7=9B=AE=E7=89=88=E6=9C=AC=E5=8F=B7=E5=B9=B6?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E5=88=B0=E6=A8=A1=E6=9D=BF=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 28 +++++++++++++++++++++++++--- pyproject.toml | 6 +++--- templates/footer.html | 2 ++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 5df3657..fa1f4ab 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,6 @@ import os +import tomllib +from pathlib import Path import dotenv from flask import Flask @@ -8,6 +10,21 @@ from storages.factory import StorageFactory dotenv.load_dotenv() + +# 从 pyproject.toml 读取版本号 +def get_version() -> str: + """从 pyproject.toml 读取项目版本号""" + try: + pyproject_path = Path(__file__).parent / "pyproject.toml" + with open(pyproject_path, "rb") as f: + pyproject_data = tomllib.load(f) + return pyproject_data.get("project", {}).get("version", "0.0.0") + except Exception: + return "0.0.0" + + +__version__ = get_version() + app = Flask(__name__) @@ -100,9 +117,7 @@ def format_timestamp(timestamp) -> str: return storage.format_timestamp(timestamp) -def generate_presigned_url( - s3_client, bucket_name: str, key: str, expires: int = None -) -> str: +def generate_presigned_url(s3_client, bucket_name: str, key: str, expires: int = None) -> str: """为指定对象生成 presigned URL(GET)。""" return storage.generate_presigned_url(key, expires) @@ -112,6 +127,13 @@ def get_file_url(key: str) -> str: return f"/file/{key}" +# 注册全局模板变量 +@app.context_processor +def inject_version(): + """向所有模板注入版本号""" + return {"app_version": __version__} + + if __name__ == "__main__": port = int(os.environ.get("PORT", 5000)) host = os.environ.get("HOST", "0.0.0.0") diff --git a/pyproject.toml b/pyproject.toml index e9f850e..3e2f1dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,10 +1,10 @@ [project] name = "Cloud-Index" -version = "0.1.0" +version = "0.9.0" description = "A cloud storage index system based on a number of cloud storage platforms" authors = [{ name = "RhenCloud", email = "i@rhen.cloud" }] readme = "README.md" -requires-python = ">=3.9" +requires-python = ">=3.11" dependencies = [ "flask>=3.1.2", "uvicorn[standard]>=0.38.0", @@ -28,7 +28,7 @@ dev = ["ruff>=0.1.6"] # build-backend = "setuptools.build_meta" [tool.ruff] -line-length = 88 +line-length = 120 select = [ "E", # pycodestyle errors "W", # pycodestyle warnings diff --git a/templates/footer.html b/templates/footer.html index b4d7dad..bbe84f2 100644 --- a/templates/footer.html +++ b/templates/footer.html @@ -5,5 +5,7 @@ GitHub · 联系 + · + v{{ app_version }}