-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathapp.py
More file actions
91 lines (75 loc) · 3.08 KB
/
Copy pathapp.py
File metadata and controls
91 lines (75 loc) · 3.08 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
# Standard Imports
import argparse
import logging
import os
# External Imports
from flask import Flask
from flask_migrate import Migrate
# Local Imports
from utils.database import init_db
from utils.database.migrations import init_migrations
from utils.routes import routes_bp
# Setup logging
logging.basicConfig(level=logging.DEBUG)
def create_app():
"""
Create and configure the Flask application.
Returns:
tuple: The configured Flask application instance and SQLAlchemy database instance.
"""
# Create the Flask app
app = Flask(__name__)
# Environment variables - ensure database is in instance directory
default_db_url = 'sqlite:////app/instance/portall.db' # Absolute path to instance directory
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', default_db_url)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = os.environ.get('SECRET_KEY', 'M1Hd4l58YKm2Tqci6ZU65sEgWDexjuSfRybf2i4G')
# Log the database URL for debugging
logging.info(f"Database URL: {app.config['SQLALCHEMY_DATABASE_URI']}")
# Initialize database
db = init_db(app)
# Initialize Flask-Migrate
migrate = Migrate(app, db)
# Register the routes blueprint
app.register_blueprint(routes_bp)
return app, db
def init_or_migrate_db(app, db):
"""
Initialize a new database or migrate an existing one.
This function uses the MigrationManager to handle all database migrations,
including both Flask-Migrate migrations and standalone migration scripts.
Args:
app (Flask): The Flask application instance.
db (SQLAlchemy): The SQLAlchemy database instance.
"""
# Initialize the migration manager and run all migrations
migration_manager = init_migrations(app, db)
logging.info("Database initialization and migration completed.")
# Create the app and get the db instance
app, db = create_app()
# Initialize auto-scan threads
def init_auto_scan_threads():
"""
Initialize the auto-scan threads for Docker, Portainer, and Komodo integrations.
"""
from utils.routes.docker import start_auto_scan_threads
with app.app_context():
start_auto_scan_threads()
logging.info("Auto-scan threads initialized")
# Run application
if __name__ == '__main__':
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Run the Portall application.')
parser.add_argument('--port', type=int, default=int(os.environ.get('PORT', 8080)),
help='Port to run the application on (default: 8080)')
parser.add_argument('--debug', action='store_true',
help='Run in debug mode')
args = parser.parse_args()
# Initialize or migrate the database before starting the app
init_or_migrate_db(app, db)
# Initialize auto-scan threads
init_auto_scan_threads()
port = args.port
debug_mode = args.debug or os.environ.get('FLASK_DEBUG', 'False').lower() == 'true'
logging.info(f"Starting Portall on port {port} with debug mode: {debug_mode}")
app.run(debug=debug_mode, host='0.0.0.0', port=port)