-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
99 lines (75 loc) · 2.39 KB
/
app.py
File metadata and controls
99 lines (75 loc) · 2.39 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
#!/usr/bin/env python3
"""
Config Backup - Switch Configuration Backup System
Backup Huawei switch configurations via SSH/Telnet with diff visualization.
"""
import os
import logging
from logging.handlers import RotatingFileHandler
from flask import Flask
from flask_cors import CORS
# Support both module and standalone execution
try:
from .routes import bp as routes_bp
from . import database
except ImportError:
from routes import bp as routes_bp
import database
def create_app():
"""Create and configure Flask application"""
# Get app directory
app_dir = os.path.dirname(os.path.abspath(__file__))
app = Flask(
__name__,
template_folder=os.path.join(app_dir, 'templates'),
static_folder=os.path.join(app_dir, 'static'),
)
# Configuration
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'change-me-in-production')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max
# Enable CORS
CORS(app, origins=["*"])
# Setup logging
setup_logging(app, app_dir)
# Initialize database
database.init_db()
# Ensure backups directory exists
backups_dir = os.path.join(app_dir, 'backups')
os.makedirs(backups_dir, exist_ok=True)
# Register blueprint
app.register_blueprint(routes_bp)
app.logger.info("Config Backup application initialized")
return app
def setup_logging(app, app_dir):
"""Setup logging with rotating file handler"""
# Create logs directory
logs_dir = os.path.join(app_dir, 'logs')
os.makedirs(logs_dir, exist_ok=True)
# File handler
file_handler = RotatingFileHandler(
os.path.join(logs_dir, 'config_backup.log'),
maxBytes=10 * 1024 * 1024, # 10MB
backupCount=10
)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s [%(name)s] %(message)s'
))
file_handler.setLevel(logging.INFO)
# Apply to app and root logger
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
# Also configure root logger for module loggers
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s [%(name)s] %(message)s',
handlers=[file_handler]
)
# Create app instance
app = create_app()
if __name__ == '__main__':
# Run development server
app.run(
host='0.0.0.0',
port=5003,
debug=True
)