diff --git a/Dockerfile.flask b/Dockerfile.flask new file mode 100644 index 0000000..63ded36 --- /dev/null +++ b/Dockerfile.flask @@ -0,0 +1,38 @@ +# Flask API 服务 Dockerfile +# ========================= +# 用于构建 Universal Data Fetcher API 服务的 Docker 镜像 + +FROM index-base:latest + +# 设置工作目录 +WORKDIR /app + +# 复制依赖文件 +COPY requirements.txt . + +# 安装依赖 +RUN uv pip install --system -r requirements.txt + +# 仅复制除 data 目录外的应用代码 +COPY . . + +# 创建日志目录 +RUN mkdir -p /app/logs + +# 设置时区为上海 +ENV TZ=Asia/Shanghai + +# 暴露 Flask 服务端口 +EXPOSE 5000 + +# 设置环境变量默认值 +ENV FLASK_APP=core/datasource/flask_server.py +ENV FLASK_ENV=production +ENV PYTHONUNBUFFERED=1 + +# 健康检查 +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ + CMD python -c "import requests; requests.get('http://localhost:5000/health')" || exit 1 + +# 启动 Flask 服务 +CMD ["python", "core/datasource/flask_server.py", "--host", "0.0.0.0", "--port", "5000"] diff --git a/docker-compose.flask.yml b/docker-compose.flask.yml new file mode 100644 index 0000000..b6fdda9 --- /dev/null +++ b/docker-compose.flask.yml @@ -0,0 +1,80 @@ +# Flask API 服务 Docker Compose 配置 +# =================================== + +version: '3.8' + +services: + etf-data-fetcher: + build: + context: . + dockerfile: Dockerfile.flask + image: 192.168.0.115:5000/etf-data-fetcher:latest + container_name: etf-data-fetcher + + ports: + - "5000:5000" + + environment: + # Flask 配置 + - FLASK_ENV=production + - PYTHONUNBUFFERED=1 + + # SSH 隧道配置(可选,启用后可获取港美股数据) + - SSH_ENABLED=${SSH_ENABLED:-false} + - SSH_HOST=${SSH_HOST:-8.218.167.69} + - SSH_PORT=${SSH_PORT:-22} + - SSH_USERNAME=${SSH_USERNAME:-root} + - SSH_KEY_PATH=${SSH_KEY_PATH:-hk_ecs.pem} + - SSH_LOCAL_PORT=${SSH_LOCAL_PORT:-1080} + + volumes: + # 环境变量配置(必需:TUSHARE_TOKEN) + - ./.env:/app/.env:ro + + # SSH 私钥(可选,获取港美股数据需要) + - ./hk_ecs.pem:/app/hk_ecs.pem:ro + + # 数据目录(可选,用于持久化缓存) + - ./data:/app/data + + # 日志目录 + - ./logs:/app/logs + + # 健康检查 + healthcheck: + test: ["CMD", "python", "-c", "import requests; requests.get('http://localhost:5000/health')"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s + + # 重启策略 + restart: unless-stopped + + # 资源限制 + deploy: + resources: + limits: + cpus: '2.0' + memory: 2G + reservations: + cpus: '0.5' + memory: 512M + + # 可选:Nginx 反向代理 + nginx: + image: nginx:alpine + container_name: etf-data-fetcher-nginx + ports: + - "80:80" + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + - etf-data-fetcher + restart: unless-stopped + profiles: + - with-nginx + +networks: + default: + name: etf-network diff --git a/requirements.txt b/requirements.txt index c77e3e4..3a05503 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,6 +15,10 @@ requests[socks]>=2.28.0 urllib3>=1.26.0 pysocks>=1.7.0 +# ==================== Web服务 ==================== +flask>=3.0.0 +flask-cors>=4.0.0 + # ==================== 进度条 ==================== tqdm>=4.65.0