Skip to content

Commit 5d2a1b3

Browse files
committed
feat: add API endpoint for serving sleep.json data to frontend
1 parent c5c7c6a commit 5d2a1b3

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

frontend/static/js/cycle-countdown.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ window.CycleCountdown = (function() {
9494
// Set the lock
9595
isFetchingData = true;
9696

97-
// Use a direct URL to the web-accessible version of sleep.json
98-
const sleepJsonUrl = window.location.origin + '/static/data/sleep.json';
97+
// Use the API endpoint to fetch sleep.json data
98+
const sleepJsonUrl = window.location.origin + '/api/sleep.json';
9999

100100
// Add a timestamp to prevent caching
101101
const url = `${sleepJsonUrl}?t=${Date.now()}`;

src/primary/routes/common.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pyotp
1212
import logging
1313
# Add render_template, send_from_directory, session
14-
from flask import Blueprint, request, jsonify, make_response, redirect, url_for, current_app, render_template, send_from_directory, session
14+
from flask import Blueprint, request, jsonify, make_response, redirect, url_for, current_app, render_template, send_from_directory, session, send_file
1515
from ..auth import (
1616
verify_user, create_session, get_username_from_session, SESSION_COOKIE_NAME,
1717
change_username as auth_change_username, change_password as auth_change_password,
@@ -20,6 +20,7 @@
2020
)
2121
from ..utils.logger import logger # Ensure logger is imported
2222
from .. import settings_manager # Import settings_manager
23+
from ..cycle_tracker import _SLEEP_DATA_PATH # Import sleep data path
2324

2425
common_bp = Blueprint('common', __name__)
2526

@@ -38,6 +39,29 @@ def logo_files(filename):
3839
logo_dir = os.path.join(common_bp.static_folder, 'logo')
3940
return send_from_directory(logo_dir, filename)
4041

42+
# --- API Routes --- #
43+
44+
@common_bp.route('/api/sleep.json', methods=['GET'])
45+
def api_get_sleep_json():
46+
"""API endpoint to directly serve the sleep.json file for frontend access"""
47+
try:
48+
if os.path.exists(_SLEEP_DATA_PATH):
49+
# Add CORS headers to allow any origin to access this resource
50+
response = send_file(_SLEEP_DATA_PATH, mimetype='application/json')
51+
response.headers.add('Access-Control-Allow-Origin', '*')
52+
return response
53+
else:
54+
# If file doesn't exist, create it and return empty object
55+
logger.info(f"[API] sleep.json not found at {_SLEEP_DATA_PATH}, creating it")
56+
os.makedirs(os.path.dirname(_SLEEP_DATA_PATH), exist_ok=True)
57+
with open(_SLEEP_DATA_PATH, 'w') as f:
58+
json.dump({}, f, indent=2)
59+
return jsonify({}), 200
60+
except Exception as e:
61+
logger.error(f"Error serving sleep.json from {_SLEEP_DATA_PATH}: {e}")
62+
# Return empty object instead of error to prevent UI breaking
63+
return jsonify({}), 200
64+
4165
# --- Authentication Routes --- #
4266

4367
@common_bp.route('/login', methods=['GET', 'POST'])

0 commit comments

Comments
 (0)