- Add HTTPSConfig model for managing HTTPS settings - Add admin routes for HTTPS configuration management - Add beautiful admin template for HTTPS configuration - Add database migration for https_config table - Add CLI utility for HTTPS management - Add setup script for automated configuration - Add Caddy configuration generator and manager - Add comprehensive documentation (3 guides) - Add HTTPS Configuration card to admin dashboard - Implement input validation and security features - Add admin-only access control with audit trail - Add real-time configuration preview - Integrate with existing Caddy reverse proxy Features: - Enable/disable HTTPS from web interface - Configure domain, hostname, IP address, port - Automatic SSL certificate management via Let's Encrypt - Real-time Caddyfile generation and reload - Full audit trail with admin username and timestamps - Support for HTTPS and HTTP fallback access points - Beautiful, mobile-responsive UI Modified files: - app/models/__init__.py (added HTTPSConfig import) - app/blueprints/admin.py (added HTTPS routes) - app/templates/admin/admin.html (added HTTPS card) - docker-compose.yml (added Caddyfile mount and admin port) New files: - app/models/https_config.py - app/blueprints/https_config.html - app/utils/caddy_manager.py - https_manager.py - setup_https.sh - migrations/add_https_config_table.py - migrations/add_email_to_https_config.py - HTTPS_STATUS.txt - Documentation files (3 markdown guides)
79 lines
2.1 KiB
Python
Executable File
79 lines
2.1 KiB
Python
Executable File
"""Logging utility for tracking system events."""
|
|
from typing import Optional
|
|
from datetime import datetime, timedelta
|
|
|
|
from app.extensions import db
|
|
from app.models.server_log import ServerLog
|
|
|
|
|
|
def log_action(level: str, message: str) -> None:
|
|
"""Log an action to the database with specified level.
|
|
|
|
Args:
|
|
level: Log level (info, warning, error)
|
|
message: Log message content
|
|
"""
|
|
try:
|
|
new_log = ServerLog(level=level, message=message)
|
|
db.session.add(new_log)
|
|
db.session.commit()
|
|
print(f"[{level.upper()}] {message}")
|
|
except Exception as e:
|
|
print(f"Error logging action: {e}")
|
|
db.session.rollback()
|
|
|
|
|
|
def get_recent_logs(limit: int = 20, level: Optional[str] = None) -> list:
|
|
"""Get the most recent log entries.
|
|
|
|
Args:
|
|
limit: Maximum number of logs to return
|
|
level: Optional filter by log level
|
|
|
|
Returns:
|
|
List of ServerLog instances
|
|
"""
|
|
query = ServerLog.query
|
|
|
|
if level:
|
|
query = query.filter_by(level=level)
|
|
|
|
return query.order_by(ServerLog.timestamp.desc()).limit(limit).all()
|
|
|
|
|
|
def clear_old_logs(days: int = 30) -> int:
|
|
"""Delete logs older than specified days.
|
|
|
|
Args:
|
|
days: Number of days to keep
|
|
|
|
Returns:
|
|
Number of logs deleted
|
|
"""
|
|
try:
|
|
cutoff_date = datetime.utcnow() - timedelta(days=days)
|
|
deleted = ServerLog.query.filter(ServerLog.timestamp < cutoff_date).delete()
|
|
db.session.commit()
|
|
log_action('info', f'Deleted {deleted} old log entries (older than {days} days)')
|
|
return deleted
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
print(f"Error clearing old logs: {e}")
|
|
return 0
|
|
|
|
|
|
# Convenience functions for specific log levels
|
|
def log_info(message: str) -> None:
|
|
"""Log an info level message."""
|
|
log_action('info', message)
|
|
|
|
|
|
def log_warning(message: str) -> None:
|
|
"""Log a warning level message."""
|
|
log_action('warning', message)
|
|
|
|
|
|
def log_error(message: str) -> None:
|
|
"""Log an error level message."""
|
|
log_action('error', message)
|