-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
474 lines (381 loc) · 16.6 KB
/
app.py
File metadata and controls
474 lines (381 loc) · 16.6 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
"""
app.py
Main entry point for the Flask application. Initializes the Flask app, configures extensions,
loads shared settings, and registers routes.
"""
# =============================================================================
# DEBUG: Show which Python is being used
# =============================================================================
import sys
print(f"🐍 Flask app starting with Python: {sys.executable}", file=sys.stderr)
print(f"🐍 Python path starts with: {sys.path[:2]}", file=sys.stderr)
# =============================================================================
# STANDARD LIBRARY IMPORTS
# =============================================================================
import os
import datetime
import hashlib
import importlib.util
from pathlib import Path
# =============================================================================
# THIRD-PARTY IMPORTS
# =============================================================================
from flask import Flask
from flask_mobility import Mobility
from flask_assets import Environment, Bundle, Filter
from flask_compress import Compress
from flask_login import LoginManager
from flask_restful import Api
from flask_wtf.csrf import CSRFProtect
# =============================================================================
# LOCAL IMPORTS
# =============================================================================
# Add current directory to path for local imports
sys.path.insert(0, str(Path(__file__).parent))
from shared import (
EXPIRE_WEEK, FLASK_DASHBOARD,
limiter, ALL_URLS, get_lock, g_c, EXPIRE_YEARS,
set_flask_restful_api, g_logger, Mode
)
from app_config import DEBUG, get_secret_key, get_dashboard_credentials
from models import User
# Define the order of JavaScript modules for loading
JS_MODULES = [
'app.js',
'infinitescroll.js',
'core.js',
'weather.js',
'chat.js',
'ai-process.js',
'config.js',
]
# Define the order of CSS modules for loading
CSS_MODULES = [
'themes.css',
'core.css',
'weather.css',
'chat.css',
'config.css',
]
# URLS_COOKIE_VERSION is now imported from shared.py
# =============================================================================
# CUSTOM FILTERS AND UTILITIES
# =============================================================================
def timestamp_to_int(timestamp):
"""
Convert a time.struct_time object to an integer timestamp.
Args:
timestamp: time.struct_time object or None
Returns:
int: Unix timestamp as integer, or 0 if timestamp is None
"""
if timestamp is None:
return 0
try:
import time
return int(time.mktime(timestamp))
except (TypeError, ValueError):
return 0
def run_one_time_last_fetch_migration(all_urls):
"""
Performs a one-time migration of last_fetch times from old cache keys to the
new unified 'all_last_fetches' cache. This is controlled by a flag to ensure
it only runs once.
Args:
all_urls (list): List of all URLs to migrate
"""
with get_lock("last_fetch_migration_lock"):
if not g_c.has('last_fetch_migration_complete'):
g_logger.info("Running one-time migration for last_fetch times...")
all_fetches = g_c.get('all_last_fetches') or {}
updated = False
for url in all_urls:
if url not in all_fetches:
old_last_fetch = g_c.get(url + ":last_fetch")
if old_last_fetch:
g_logger.info(f"Migrating last_fetch for {url}.")
all_fetches[url] = old_last_fetch
updated = True
if updated:
g_c.put('all_last_fetches', all_fetches, timeout=EXPIRE_YEARS)
# Set the flag to indicate migration is complete
g_c.put('last_fetch_migration_complete', True, timeout=EXPIRE_YEARS)
g_logger.info("Last_fetch migration complete.")
def detect_mode(forced_mode=None):
"""
Detect the current mode based on working directory and settings files.
Args:
forced_mode (str, optional): If provided, force this mode instead of detecting from cwd.
Returns:
tuple: (mode_string, settings_module, settings_config)
"""
# If mode is forced, use it directly
if forced_mode:
forced_mode = forced_mode.lower()
g_logger.info(f"Forcing mode: {forced_mode}")
# Validate that the forced mode is a valid Mode enum value
valid_modes = [mode.value for mode in Mode]
if forced_mode not in valid_modes:
g_logger.error(f"Invalid mode '{forced_mode}'. Valid modes are: {', '.join(valid_modes)}")
sys.exit(1)
# Load the settings file for the forced mode
settings_file = f"{forced_mode}_report_settings.py"
if not os.path.isfile(settings_file):
g_logger.error(f"Settings file not found for mode '{forced_mode}': {settings_file}")
sys.exit(1)
spec = importlib.util.spec_from_file_location("module_name", settings_file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
g_logger.info(f"Loaded settings for forced mode '{forced_mode}' from {settings_file}")
return forced_mode, module, module.CONFIG
# Otherwise, use the original detection logic based on cwd
cwd = os.getcwd()
g_logger.info(f"Current working directory: {cwd}")
for mode_enum_val in Mode:
settings_file = f"{mode_enum_val.value}_report_settings.py"
if os.path.isfile(settings_file):
spec = importlib.util.spec_from_file_location("module_name", settings_file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if cwd == module.CONFIG.PATH:
g_logger.info(f"Matched mode '{mode_enum_val.value}' with path {module.CONFIG.PATH}")
return mode_enum_val.value, module, module.CONFIG
g_logger.error(f"Could not determine mode from current directory: {cwd}")
g_logger.error("Expected to find a settings file with a matching PATH in the current directory.")
g_logger.error("Use --mode <mode_name> to force a specific mode.")
sys.exit(1)
# =============================================================================
# FLASK APPLICATION INITIALIZATION
# =============================================================================
def create_app():
"""
Factory function to create and configure the Flask application.
Returns:
Flask: Configured Flask application instance
"""
# Initialize Flask app
app = Flask(__name__)
# Apply basic configuration
app.config.update({
'SEND_FILE_MAX_AGE_DEFAULT': EXPIRE_WEEK,
'MAX_CONTENT_LENGTH': 5 * 1024 * 1024, # 5MB upload limit
'DEBUG': DEBUG,
'SECRET_KEY': get_secret_key()
})
# Register custom Jinja2 filters
app.jinja_env.filters['timestamp_to_int'] = timestamp_to_int
return app
# Create the main application instance
g_app = create_app()
# Logging is now handled in shared.py and imported
# =============================================================================
# FLASK EXTENSIONS INITIALIZATION
# =============================================================================
def initialize_extensions(app):
"""
Initialize all Flask extensions.
Args:
app (Flask): Flask application instance
"""
# Initialize Flask-Mobility for mobile detection
Mobility(app)
# Initialize Flask-Compress for response compression
# Compress(app)
# NOTE: Compression is currently handled by Apache mod_deflate/mod_brotli at the server level,
# which is more efficient than application-level compression. If deploying with Gunicorn,
# nginx, or another WSGI server without compression, uncomment the line above to enable
# Flask-Compress for gzip/brotli compression of Flask responses only.
# Initialize Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
login_manager.login_message = 'Please log in to access this page.'
@login_manager.user_loader
def load_user(user_id):
"""Load user for Flask-Login."""
return User.get(user_id)
# Initialize rate limiter
limiter.init_app(app)
# Initialize Flask-RESTful API
api_instance = Api(app)
set_flask_restful_api(api_instance)
# Initialize Flask-WTF CSRF protection
csrf = CSRFProtect()
csrf.init_app(app)
# Disable CSRF protection for API endpoints
app.config['WTF_CSRF_ENABLED'] = False
return login_manager
# Initialize extensions
g_login_manager = initialize_extensions(g_app)
# =============================================================================
# MONITORING DASHBOARD CONFIGURATION
# =============================================================================
def setup_monitoring_dashboard(app):
"""
Set up Flask-MonitoringDashboard if enabled.
Args:
app (Flask): Flask application instance
"""
if not FLASK_DASHBOARD:
return
try:
import flask_monitoringdashboard as dashboard
from flask_monitoringdashboard.core.config import Config
# Configure Flask-MonitoringDashboard
dashboard_credentials = get_dashboard_credentials()
config = Config()
config.USERNAME = dashboard_credentials.get('username')
config.PASSWORD = dashboard_credentials.get('password')
config.DATABASE = 'sqlite:///flask_monitoringdashboard.db'
# Initialize with custom configuration
dashboard.bind(app, config)
g_logger.info("Flask-MonitoringDashboard initialized successfully")
except ImportError:
g_logger.warning("Flask-MonitoringDashboard not available")
except (AttributeError, TypeError) as e:
g_logger.warning(f"Failed to initialize Flask-MonitoringDashboard: {e}")
# Set up monitoring dashboard
setup_monitoring_dashboard(g_app)
# =============================================================================
# ASSET BUNDLING CONFIGURATION
# =============================================================================
def setup_asset_bundles(app):
"""
Set up Flask-Assets for JavaScript and CSS bundling.
Args:
app (Flask): Flask application instance
Returns:
tuple: (js_bundle, css_bundle) - Configured asset bundles
"""
# Initialize Flask-Assets
g_assets = Environment(app)
g_assets.url = app.static_url_path
# Create JS bundle from individual modules
js_files = [
str(Path(__file__).parent / 'templates' / module)
for module in JS_MODULES
]
# Register JavaScript bundle
g_js_bundle = g_assets.register('js_all', Bundle(
*js_files,
output='linuxreport.js'
))
# Create CSS bundle from individual modules
css_files = [
str(Path(__file__).parent / 'templates' / module)
for module in CSS_MODULES
]
# Register CSS bundle for cache busting
g_css_bundle = g_assets.register('css_all', Bundle(
*css_files,
output='linuxreport.css'
))
# Make assets available to templates
app.jinja_env.globals['assets'] = g_assets
return g_assets, g_js_bundle, g_css_bundle
# Set up asset bundles
g_assets, g_js_bundle, g_css_bundle = setup_asset_bundles(g_app)
# =============================================================================
# APPLICATION STARTUP TASKS
# =============================================================================
def _process_bundle(app, bundle, output_name, modules, comment_style='js'):
"""
Process an asset bundle: check hash, rebuild if changed, and add header.
Args:
app: Flask application instance
bundle: The asset bundle to process
output_name: Filename of the output bundle (e.g., 'linuxreport.js')
modules: List of module filenames included in the bundle
comment_style: 'js' for // or 'css' for /* ... */
"""
output_path = Path(app.static_folder) / output_name
label = "JavaScript" if comment_style == 'js' else "CSS"
# 1. Get existing hash from header
existing_hash = None
if output_path.exists():
try:
with open(output_path, 'r', encoding='utf-8') as f:
# Read first few lines to find hash
for _ in range(5):
line = f.readline()
if not line: break
prefix = '// Hash: ' if comment_style == 'js' else '/* Hash: '
if line.startswith(prefix):
existing_hash = line.split(prefix)[1].strip().split(' ')[0]
break
except IOError as e:
g_logger.warning(f"Could not read existing {label} bundle: {e}")
# 2. Calculate hash of source files
source_content = ""
for module in modules:
source_path = Path(__file__).parent / 'templates' / module
if source_path.exists():
with open(source_path, 'r', encoding='utf-8') as f:
source_content += f.read()
new_hash = hashlib.md5(source_content.encode('utf-8')).hexdigest()[:8]
# 3. Only rebuild if hash changed or file doesn't exist
if not existing_hash or existing_hash != new_hash:
if output_path.exists():
output_path.unlink()
g_logger.info(f"Removed existing {label} bundle for fresh build")
bundle.build()
g_logger.info(f"{label} bundle built successfully")
# Add header information
if output_path.exists():
with open(output_path, 'r', encoding='utf-8') as f:
content = f.read()
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if comment_style == 'js':
header = f'// Compiled: {timestamp}\n'
header += f'// Hash: {new_hash}\n'
header += f'// Source files: {", ".join(modules)}\n\n'
else:
header = f'/* Compiled: {timestamp} */\n'
header += f'/* Hash: {new_hash} */\n'
header += f'/* Source files: {", ".join(modules)} */\n\n'
with open(output_path, 'w', encoding='utf-8') as f:
f.write(header + content)
g_logger.info(f"{label} content changed (new hash: {new_hash}), updated with new header")
else:
g_logger.warning(f"{label} bundle was not created after build")
else:
g_logger.info(f"{label} content unchanged (hash: {new_hash}), reusing existing file")
def perform_startup_tasks(app, js_bundle, css_bundle):
"""
Perform necessary startup tasks like building assets and running migrations.
Args:
app (Flask): Flask application instance
js_bundle: JavaScript asset bundle
css_bundle: CSS asset bundle
"""
with app.app_context():
try:
# Process JavaScript bundle
_process_bundle(app, js_bundle, 'linuxreport.js', JS_MODULES, comment_style='js')
# Process CSS bundle
_process_bundle(app, css_bundle, 'linuxreport.css', CSS_MODULES, comment_style='css')
# Run one-time migration of last_fetch times
run_one_time_last_fetch_migration(ALL_URLS.keys())
except (IOError, OSError, RuntimeError) as e:
g_logger.warning(f"Failed to complete startup tasks: {e}")
# Perform startup tasks
perform_startup_tasks(g_app, g_js_bundle, g_css_bundle)
# =============================================================================
# ROUTE REGISTRATION
# =============================================================================
# Import and initialize routes (avoid circular import)
from routes import init_app
init_app(g_app)
# =============================================================================
# DEBUG MODE WARNINGS
# =============================================================================
if DEBUG or g_app.debug:
g_logger.warning("Application is running in debug mode")
# =============================================================================
# WSGI ENTRY POINT
# =============================================================================
# WSGI entry point - set after all initialization is complete
# This ensures the application is fully configured before being used by WSGI servers
# Compatible with both mod_wsgi (Apache) and Gunicorn
# Use 'app:application' as the WSGI callable in Gunicorn configs
application = g_app