设置 (services/settings.py, routes/settings.py) - user_settings 表:身高/体重/出生日期/性别/单位/自动同步开关/同步频率/历史范围 - GET|PUT /api/settings,GET /api/settings/options(取值由后端给,前端不臆造) - GET /api/settings/rating-basis:把每条参考区间的来源公开出来。 一个把数字标成「偏低」的区间是在下判断,用户有权看到依据。 运动详情 (services/garmin.py) - GET /api/garmin/activities/<id>/detail:概览/分段/心率区间/天气/装备/采样曲线 - 首次打开回源 Garmin 并落库,之后走缓存;?refresh=1 强制刷新 - 采样点在写入时抽稀到 300,手机图表画不了更多,也免得整行撑大 身体年龄 (services/fitness_age.py) - 0.2.8 版 garminconnect 没有 fitnessage 接口,改为本地按公开常模推算: VO₂max 对应年龄为基准,静息心率与 BMI 做有上限的修正 - 返回每一步的中间值,界面照实展示,不做成一个不可追溯的分数 - 高于参考表最年轻一档时按 20 岁计——那里外推会得到「11 岁」这种结果 调度器改为每 5 分钟 tick,是否该同步按各账号自己的频率判断 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
"""
|
|
Flask application factory for Garmin Health Lab.
|
|
|
|
Run directly (`python app.py`) for development, or serve with Gunicorn:
|
|
gunicorn wsgi:app -b 0.0.0.0:5000
|
|
"""
|
|
import os
|
|
|
|
from flask import Flask, jsonify, send_from_directory
|
|
from flask_cors import CORS
|
|
|
|
import db
|
|
from config import CORS_ORIGINS, PORT, STATIC_DIR
|
|
from routes import auth, garmin, health, analysis, settings
|
|
from services import scheduler
|
|
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
CORS(app, resources={r"/api/*": {"origins": CORS_ORIGINS}}, supports_credentials=True)
|
|
|
|
# Create tables once at startup (idempotent).
|
|
db.init_db()
|
|
|
|
# Keeps the database current without the user pressing anything. Safe to
|
|
# call in every worker: the job is claimed through the database, so only
|
|
# one of them actually runs a given tick.
|
|
scheduler.start()
|
|
|
|
# In production the built React app is served by this same process, so the
|
|
# deployment is a single port with no reverse proxy to configure. In
|
|
# development STATIC_DIR does not exist and the CRA dev server serves the
|
|
# UI instead — hence the guard rather than an unconditional route.
|
|
has_ui = bool(STATIC_DIR) and os.path.isfile(os.path.join(STATIC_DIR, "index.html"))
|
|
|
|
@app.route("/")
|
|
def index():
|
|
if has_ui:
|
|
return send_from_directory(STATIC_DIR, "index.html")
|
|
return jsonify({"name": "Garmin Health Lab API", "version": "1.0.0"})
|
|
|
|
@app.route("/api/health/status")
|
|
def health_status():
|
|
return jsonify({"status": "ok", "db": db.DB_TYPE})
|
|
|
|
app.register_blueprint(auth.bp, url_prefix="/api/auth")
|
|
app.register_blueprint(garmin.bp, url_prefix="/api/garmin")
|
|
app.register_blueprint(health.bp, url_prefix="/api/health")
|
|
app.register_blueprint(analysis.bp, url_prefix="/api/analysis")
|
|
app.register_blueprint(settings.bp, url_prefix="/api/settings")
|
|
|
|
@app.errorhandler(404)
|
|
def not_found(_e):
|
|
# API paths always answer in JSON. Everything else falls through to the
|
|
# SPA so client-side routes (/settings, /recommendations, ...) survive a
|
|
# page reload instead of 404-ing.
|
|
from flask import request
|
|
|
|
if has_ui and not request.path.startswith("/api/"):
|
|
asset = request.path.lstrip("/")
|
|
if asset and os.path.isfile(os.path.join(STATIC_DIR, asset)):
|
|
return send_from_directory(STATIC_DIR, asset)
|
|
return send_from_directory(STATIC_DIR, "index.html")
|
|
return jsonify({"error": "not found"}), 404
|
|
|
|
@app.errorhandler(500)
|
|
def server_error(_e):
|
|
return jsonify({"error": "internal error"}), 500
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=PORT, debug=False)
|