-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.py
More file actions
66 lines (50 loc) · 2.02 KB
/
Copy pathsystem.py
File metadata and controls
66 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from flask import Blueprint, current_app, jsonify, send_from_directory
import logging
logger = logging.getLogger('SchoolGradesServer.System')
bp = Blueprint('system', __name__)
ALLOWED_STATIC_EXT = {
'.html', '.css', '.js', '.json', '.png', '.jpg', '.jpeg', '.gif',
'.svg', '.ico', '.woff', '.woff2', '.webmanifest', '.ttf', '.xml', '.txt',
}
@bp.route('/')
def index():
logger.debug('Accessing index page')
response = send_from_directory('public', 'index.html')
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
return response
@bp.route('/<path:filename>')
def static_files(filename):
ext = os.path.splitext(filename)[1].lower()
if ext not in ALLOWED_STATIC_EXT:
return jsonify({'error': 'Forbidden'}), 403
if any(part.startswith('.') for part in filename.split('/')):
return jsonify({'error': 'Forbidden'}), 403
response = send_from_directory('public', filename)
if ext in ('.js', '.css'):
response.cache_control.max_age = 31536000
elif ext in ('.woff', '.woff2', '.ttf', '.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico', '.webmanifest'):
response.cache_control.max_age = 86400
return response
@bp.route('/health')
def health_check():
redis_client = current_app.config.get('REDIS_CLIENT')
redis_status = 'not_configured'
if redis_client:
try:
redis_client.ping()
redis_status = 'connected'
except Exception as e:
logger.error(f'Health check: Redis ping failed: {e}', exc_info=True)
return jsonify({
'status': 'error',
'redis': 'disconnected',
'detail': '伺服器內部錯誤',
}), 503
return jsonify({'status': 'ok', 'redis': redis_status}), 200
@bp.route('/api/turnstile-config')
def turnstile_config():
site_key = current_app.config.get('TURNSTILE_SITE_KEY', '')
return jsonify({'siteKey': site_key})