Shuiwu Zhu commited on
Commit ·
e4e1a73
1
Parent(s): 27c62df
deploy to HF Spaces
Browse files- Dockerfile +7 -1
- config/setting.toml +1 -1
- config/setting_example.toml +1 -1
- src/core/config.py +5 -0
- src/core/database.py +8 -3
- src/main.py +5 -0
Dockerfile
CHANGED
|
@@ -8,6 +8,12 @@ RUN pip install --no-cache-dir --root-user-action=ignore -r requirements.txt
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
CMD ["python", "main.py"]
|
|
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
+
# HF Spaces 持久化存储目录
|
| 12 |
+
ENV DATA_DIR=/data
|
| 13 |
+
RUN mkdir -p /data
|
| 14 |
+
|
| 15 |
+
# HF Spaces 要求端口 7860
|
| 16 |
+
ENV PORT=7860
|
| 17 |
+
EXPOSE 7860
|
| 18 |
|
| 19 |
CMD ["python", "main.py"]
|
config/setting.toml
CHANGED
|
@@ -12,7 +12,7 @@ max_poll_attempts = 200
|
|
| 12 |
|
| 13 |
[server]
|
| 14 |
host = "0.0.0.0"
|
| 15 |
-
port =
|
| 16 |
|
| 17 |
[debug]
|
| 18 |
enabled = false
|
|
|
|
| 12 |
|
| 13 |
[server]
|
| 14 |
host = "0.0.0.0"
|
| 15 |
+
port = 7860
|
| 16 |
|
| 17 |
[debug]
|
| 18 |
enabled = false
|
config/setting_example.toml
CHANGED
|
@@ -12,7 +12,7 @@ max_poll_attempts = 200
|
|
| 12 |
|
| 13 |
[server]
|
| 14 |
host = "0.0.0.0"
|
| 15 |
-
port =
|
| 16 |
|
| 17 |
[debug]
|
| 18 |
enabled = false
|
|
|
|
| 12 |
|
| 13 |
[server]
|
| 14 |
host = "0.0.0.0"
|
| 15 |
+
port = 7860
|
| 16 |
|
| 17 |
[debug]
|
| 18 |
enabled = false
|
src/core/config.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
"""Configuration management for Flow2API"""
|
|
|
|
| 2 |
import tomli
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Dict, Any, Optional
|
|
@@ -74,6 +75,10 @@ class Config:
|
|
| 74 |
|
| 75 |
@property
|
| 76 |
def server_port(self) -> int:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return self._config["server"]["port"]
|
| 78 |
|
| 79 |
@property
|
|
|
|
| 1 |
"""Configuration management for Flow2API"""
|
| 2 |
+
import os
|
| 3 |
import tomli
|
| 4 |
from pathlib import Path
|
| 5 |
from typing import Dict, Any, Optional
|
|
|
|
| 75 |
|
| 76 |
@property
|
| 77 |
def server_port(self) -> int:
|
| 78 |
+
# 优先使用环境变量 PORT(HF Spaces 等云平台通过此变量指定端口)
|
| 79 |
+
env_port = os.environ.get("PORT")
|
| 80 |
+
if env_port:
|
| 81 |
+
return int(env_port)
|
| 82 |
return self._config["server"]["port"]
|
| 83 |
|
| 84 |
@property
|
src/core/database.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
"""Database storage layer for Flow2API"""
|
|
|
|
| 2 |
import aiosqlite
|
| 3 |
import json
|
| 4 |
from datetime import datetime
|
|
@@ -12,9 +13,13 @@ class Database:
|
|
| 12 |
|
| 13 |
def __init__(self, db_path: str = None):
|
| 14 |
if db_path is None:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
db_path = str(data_dir / "flow.db")
|
| 19 |
self.db_path = db_path
|
| 20 |
|
|
|
|
| 1 |
"""Database storage layer for Flow2API"""
|
| 2 |
+
import os
|
| 3 |
import aiosqlite
|
| 4 |
import json
|
| 5 |
from datetime import datetime
|
|
|
|
| 13 |
|
| 14 |
def __init__(self, db_path: str = None):
|
| 15 |
if db_path is None:
|
| 16 |
+
# 优先使用环境变量 DATA_DIR(HF Spaces 持久化目录为 /data)
|
| 17 |
+
env_data_dir = os.environ.get("DATA_DIR")
|
| 18 |
+
if env_data_dir:
|
| 19 |
+
data_dir = Path(env_data_dir)
|
| 20 |
+
else:
|
| 21 |
+
data_dir = Path(__file__).parent.parent.parent / "data"
|
| 22 |
+
data_dir.mkdir(parents=True, exist_ok=True)
|
| 23 |
db_path = str(data_dir / "flow.db")
|
| 24 |
self.db_path = db_path
|
| 25 |
|
src/main.py
CHANGED
|
@@ -197,6 +197,11 @@ app.add_middleware(
|
|
| 197 |
app.include_router(routes.router)
|
| 198 |
app.include_router(admin.router)
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
# Static files - serve tmp directory for cached files
|
| 201 |
tmp_dir = Path(__file__).parent.parent / "tmp"
|
| 202 |
tmp_dir.mkdir(exist_ok=True)
|
|
|
|
| 197 |
app.include_router(routes.router)
|
| 198 |
app.include_router(admin.router)
|
| 199 |
|
| 200 |
+
# 健康检查端点(供 UptimeRobot 等保活服务 ping,无需认证)
|
| 201 |
+
@app.get("/health")
|
| 202 |
+
async def health_check():
|
| 203 |
+
return {"status": "ok"}
|
| 204 |
+
|
| 205 |
# Static files - serve tmp directory for cached files
|
| 206 |
tmp_dir = Path(__file__).parent.parent / "tmp"
|
| 207 |
tmp_dir.mkdir(exist_ok=True)
|