function debug_cache_info
Flask debug endpoint that provides comprehensive cache busting information including static file versions, modification times, and cache buster values.
/tf/active/vicechatdev/vice_ai/app.py
1666 - 1694
moderate
Purpose
This endpoint serves as a debugging tool for developers to inspect cache busting mechanisms in a Flask application. It returns detailed information about static files (CSS and JS), their versions, modification times, file sizes, and the current cache buster configuration. This is useful for troubleshooting cache-related issues during development and ensuring proper cache invalidation strategies are working correctly.
Source Code
def debug_cache_info():
"""Debug endpoint to show cache busting information"""
if not DEBUG_MODE:
return jsonify({'error': 'Debug mode not enabled'}), 404
static_files = {}
if app.static_folder and os.path.exists(app.static_folder):
for root, dirs, files in os.walk(app.static_folder):
for file in files:
if file.endswith(('.css', '.js')):
relative_path = os.path.relpath(os.path.join(root, file), app.static_folder)
full_path = os.path.join(root, file)
try:
mtime = os.path.getmtime(full_path)
static_files[relative_path] = {
'version': get_file_version(relative_path),
'mtime': datetime.fromtimestamp(mtime).isoformat(),
'size': os.path.getsize(full_path)
}
except OSError:
static_files[relative_path] = {'error': 'Could not read file stats'}
return jsonify({
'debug_mode': DEBUG_MODE,
'cache_buster': get_cache_buster(),
'startup_version': CACHE_BUST_VERSION,
'static_files': static_files,
'timestamp': datetime.now().isoformat()
})
Return Value
Returns a Flask JSON response containing: 'debug_mode' (boolean indicating if debug is enabled), 'cache_buster' (current cache buster value from get_cache_buster()), 'startup_version' (CACHE_BUST_VERSION constant), 'static_files' (dictionary mapping relative file paths to objects with 'version', 'mtime', and 'size' keys), and 'timestamp' (ISO format timestamp of when the endpoint was called). If DEBUG_MODE is False, returns a 404 error with JSON error message.
Dependencies
flaskosdatetime
Required Imports
from flask import Flask
from flask import jsonify
import os
from datetime import datetime
Usage Example
# Assuming Flask app is configured with required globals and functions
# app = Flask(__name__)
# DEBUG_MODE = True
# CACHE_BUST_VERSION = '1.0.0'
#
# def get_cache_buster():
# return 'v1234567890'
#
# def get_file_version(filepath):
# return hashlib.md5(filepath.encode()).hexdigest()[:8]
@app.route('/debug/cache-info')
def debug_cache_info():
# Function implementation here
pass
# Access the endpoint:
# GET http://localhost:5000/debug/cache-info
# Response:
# {
# "debug_mode": true,
# "cache_buster": "v1234567890",
# "startup_version": "1.0.0",
# "static_files": {
# "css/style.css": {
# "version": "a1b2c3d4",
# "mtime": "2024-01-15T10:30:00",
# "size": 2048
# }
# },
# "timestamp": "2024-01-15T12:00:00"
# }
Best Practices
- This endpoint should only be enabled in development/debug mode and never in production environments
- Ensure DEBUG_MODE is properly configured to prevent unauthorized access to internal file information
- The endpoint walks the entire static folder tree, which could be slow for large applications - consider caching results if needed
- Handle OSError exceptions gracefully when files cannot be accessed
- Consider adding authentication/authorization checks even in debug mode to prevent information disclosure
- The endpoint returns 404 when DEBUG_MODE is False, which is a security best practice to hide debug endpoints in production
- Ensure get_file_version() and get_cache_buster() functions are implemented before using this endpoint
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function check_debug_endpoint 73.4% similar
-
function get_cache_buster 72.2% similar
-
function inject_cache_buster 69.8% similar
-
function get_file_version 68.4% similar
-
function debug_session_results 64.2% similar