Files
GarminHealthLab/backend/.env.example
ericwyuan 9d6ebbe422 ops: 生产从 NAS 整体迁移到甲骨文云主机
NAS 局域网 IP 因重启被 DHCP 换过两次,磁盘/网络稳定性都不如已经跑着好几个
生产服务的甲骨文机器。整体搬迁:应用 + 数据库都搬走,NAS 只保留 Gitea(这个
仓库的源码托管,未动)。

## 迁移过程(已核对无损)

- MariaDB:NAS 导出(10.11 源库,处理了只有新版本才有的 `/*M!999999` 注释)
  → 导入甲骨文 MariaDB 10.3.39,**20 张表逐条精确 COUNT(*) 比对完全一致**
- 冻结 NAS(停服务)后又 dump 一次核对,确认期间零数据差异,才继续删库
- NAS `garmin_health_lab` 已 DROP DATABASE,备份在本地
  `~/Desktop/Work/backups/garmin_health_lab_nas_backup_20260912.sql.gz`
- 应用部署到 `/opt/garmin-health-lab`,systemd 单元(`ubuntu` 用户,非
  root),和这台机器上的 ai-gateway/auth-hub 同一套约定
- 公网:`https://garmin.zichuan.xyz`,DNS + Caddy 反代 + 自动 TLS,替代原来
  `NAS frpc → 甲骨文:8124` 那条隧道(已从 NAS 的 frpc.toml 精确删除对应段,
  其它转发未动,改完逐条复检过没打断)
- auth-hub 回调地址换成新域名,NAS/旧端口那几条历史回调已清掉
- AI 网关配置改本地回环(网关现在同机了),触发真实生成验证过

## 一个当场拦下来的风险

甲骨文部署完默认开着自动同步。迁移窗口期两边并行跑时,若两边的调度器同时去
刷新 Garmin 令牌,会撞上按账号计算的 SSO 限流(`GarminHealthLab` 仓库
2026-09-03 那次事故的根因,那次修复花了一整天)。确认账号级 auto_sync 设置
本来是关的、这次算侥幸没撞上——不是设计上的保险,所以迁移期间显式在甲骨文这边
加了 `AUTO_SYNC=false`,直接在运行进程里验证过生效,确认 NAS 已冻结、数据无
缺口后才打开。

## 文档 / 脚本同步

CLAUDE.md 明确写过"部署位置会变,排障前先查、不要凭记忆"——这次是第二次踩中
同一类问题(上次是"NAS 有没有生产环境"判断错),所以把 CLAUDE.md / PROGRESS.md
/ README.md / docs/* 里的部署事实全部更新,NAS 时代的 `deploy/` 脚本加废弃
说明保留参考、不删除,新增 `deploy/push_oracle.sh`(当场跑通一次真实部署)。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-12 23:53:15 +08:00

124 lines
5.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# --- Server ---
# BACKEND_PORT takes precedence over PORT. Prefer it: many tools inject PORT
# for the frontend, and Flask would otherwise take the React dev server's port.
BACKEND_PORT=5000
# --- Database: sqlite (default) or mariadb ---
DB_TYPE=sqlite
# SQLite file (used when DB_TYPE=sqlite)
DATABASE_PATH=./data/health.db
# MariaDB (used when DB_TYPE=mariadb) — production DB is co-located with the
# app on the Oracle box (129.146.26.249, MariaDB 10.3.39), TCP 127.0.0.1:3306,
# dedicated account (not root). MariaDB treats `user@localhost` and
# `user@127.0.0.1` as two different accounts — if you create this user by
# hand, create both host variants with the same password, or TCP connections
# fail with a password that looks right but is not the one that account has.
# MARIADB_HOST=127.0.0.1
# MARIADB_PORT=3306
# MARIADB_USER=garmin
# MARIADB_PASSWORD=your_production_mariadb_password
# MARIADB_DATABASE=garmin_health_lab
# --- Auth ---
# CHANGE THIS in production! Used to sign JWTs (7-day expiry by default).
JWT_SECRET=dev_secret_change_me
JWT_EXPIRY_DAYS=7
# --- auth-hub OAuth2 provider (centralized SSO) ---
# See docs/AUTH_HUB_INTEGRATION.md for setup instructions.
#
# Base URL of the auth-hub service
AUTH_HUB_BASE_URL=http://129.146.26.249:5300
#
# OAuth2 client credentials (obtain from auth-hub.manage_clients create)
# NOTE: these must be registered against the auth-hub instance AUTH_HUB_BASE_URL
# actually points to (dev vs prod are separate databases with separate clients).
# Put the REAL values in backend/.env (gitignored) — never here.
AUTH_HUB_CLIENT_ID=your_client_id
AUTH_HUB_CLIENT_SECRET=your_client_secret
#
# Callback URL (must exactly match what's registered in auth-hub). Production
# registers https://garmin.zichuan.xyz/auth/callback — add/remove redirect
# URIs on that client with /opt/auth-hub's manage_clients CLI, not by editing
# auth-hub's own database directly.
AUTH_HUB_REDIRECT_URI=http://127.0.0.1:5500/auth/callback
# --- CORS (comma-separated allowed front-end origins) ---
# localhost stays in the production list on purpose: CORS is not an auth
# boundary — every data route requires a valid JWT — so allowing a developer's
# dev server costs nothing and saves toggling this on every session.
CORS_ORIGIN=http://localhost:3000,http://localhost:5173
# --- AI models (text-only, large context) ---
# Put REAL keys in backend/.env — that file is gitignored. Never commit keys.
# Any model whose credentials are absent is skipped automatically.
# Self-hosted AI gateway (model id "gateway"). OpenAI-compatible; it fans out
# over nvidia/gemini/ollama itself and rotates several Gemini keys, so it
# absorbs single-vendor quota limits. Reached directly, bypassing any local
# HTTP proxy. NOTE: its NVIDIA upstream is a large reasoning model — replies
# can take 2-3 minutes, so set AI_TIMEOUT_SECONDS accordingly.
# Production (co-located with ai-gateway on the same Oracle box) uses the
# loopback address — skips Caddy and the public hop entirely, and the token
# never leaves localhost either way. Use the HTTPS domain instead only when
# this app runs somewhere else: the token rides in an Authorization header
# and must not cross the public internet in the clear.
AI_GATEWAY_BASE_URL=http://127.0.0.1:5100/v1
AI_GATEWAY_TOKEN=
AI_GATEWAY_MODEL=ai-gateway-auto
# Google AI Studio -> "gemini-flash". Free-tier quota is small; 429s are common.
GEMINI_API_KEY=
# NVIDIA NIM -> "llama-70b", "nemotron-49b", "mistral-large".
# Model ids come from that account's live GET /v1/models — do not guess them.
NVIDIA_API_KEY=
# NVIDIA_BASE_URL=https://integrate.api.nvidia.com/v1
# Preference order. The first configured model answers; if it fails or times
# out, the next is tried. Read per request, so changes need no restart.
AI_MODEL_CHAIN=gateway,gemini-flash,llama-70b
# Max days of history sent (CSV-encoded). Trimmed further per model so the
# payload always fits that model's own context window.
AI_DAY_BUDGET=365
# Measured against the gateway, not guessed: a trivial prompt took 138s end to
# end, because its primary upstream emits a full chain of thought before the
# answer. Nothing user-facing blocks on this (the briefing generates in a
# background thread), but the timeout still has to clear the real latency.
AI_TIMEOUT_SECONDS=300
# Output cap. Reasoning models spend part of it thinking before they answer;
# entries that need more declare their own budget in services/ai.py.
AI_MAX_TOKENS=1024
# Output cap for the AI coach (晨报 / 趋势归因 / Copilot). Larger than
# AI_MAX_TOKENS above: the same reasoning trace is spent from this budget
# before the answer starts, and at 1024 the reply was all thinking with the
# JSON truncated away.
AI_COACH_MAX_TOKENS=4000
# --- AI coach job queue ---
# Three projects share the gateway, so going too high causes 502s.
AI_JOB_CONCURRENCY=2
AI_JOB_GAP_SECONDS=5
# Set AI_JOBS=false to stop consuming entirely (screens then show the computed
# figures with no model reading).
AI_JOBS=true
# --- Garmin 请求节流 ---
# 一天的历史要 9 次 API 调用_extract_daily 6 + daily_extras 3短同步再加
# 5 次曲线,每条没有详情的运动 1 次。730 天全历史 ≈ 6600 个请求。以前是能发多
# 快发多快。
#
# 0.5 秒的依据2026-09-03 调研,见 services/garmin_throttle.py 顶部注释):
# sirredbeard/garmin-data-export 默认 0.15s、evg656e/garmin-connect-scraper
# 默认 3s、佳明官方合作方 API 是 100 次/分钟(合 0.6s)。
GARMIN_MIN_INTERVAL_SECONDS=0.5
# 单次同步的请求预算。跑满就干净收尾,下次接着跑(已存的天数会跳过)。
# 1200 × 0.5s ≈ 10 分钟,够覆盖四个月历史。
GARMIN_REQUEST_BUDGET=1200
# 每次同步补多少条运动详情。新账号有几百条,不限量就是一次性打光预算。
GARMIN_DETAILS_PER_SYNC=40