mirror of
https://github.com/RhenCloud/Cloud-Index.git
synced 2025-12-06 07:06:41 +08:00
feat: 从 pyproject.toml 读取项目版本号并注入到模板中
This commit is contained in:
28
app.py
28
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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,5 +5,7 @@
|
||||
<a href="https://github.com/RhenCloud/Cloud-Index" target="_blank" rel="noopener">GitHub</a>
|
||||
<span>·</span>
|
||||
<a href="mailto:i@rhen.cloud">联系</a>
|
||||
<span>·</span>
|
||||
<span style="color: var(--secondary-text)">v{{ app_version }}</span>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
Reference in New Issue
Block a user