Replace MySQL with SQLite, fix Jinja2 template syntax, and add README
This commit is contained in:
27
app/app.py
27
app/app.py
@@ -7,8 +7,6 @@ from datetime import datetime, timezone
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
import paramiko
|
||||
import pymysql
|
||||
pymysql.install_as_MySQLdb()
|
||||
|
||||
from flask import Flask, render_template, jsonify
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
@@ -16,12 +14,10 @@ from flask_sqlalchemy import SQLAlchemy
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DB_PATH = '/app/data/infmap.db'
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = (
|
||||
f"mysql+pymysql://{os.environ['MYSQL_USER']}:{os.environ['MYSQL_PASSWORD']}"
|
||||
f"@{os.environ.get('MYSQL_HOST', 'db')}/{os.environ['MYSQL_DATABASE']}"
|
||||
)
|
||||
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle': 280, 'pool_pre_ping': True}
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
COLLECTION_INTERVAL = int(os.environ.get('COLLECTION_INTERVAL', 300))
|
||||
@@ -349,19 +345,10 @@ def usage_color(percent):
|
||||
# --- Main ---
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Wait for database
|
||||
for attempt in range(30):
|
||||
try:
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
logger.info("Database ready")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.info("Waiting for database... (%s)", e)
|
||||
time.sleep(2)
|
||||
else:
|
||||
logger.error("Could not connect to database after 30 attempts")
|
||||
exit(1)
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
logger.info("Database ready at %s", DB_PATH)
|
||||
|
||||
# Start collector thread
|
||||
collector_thread = threading.Thread(target=collector_loop, daemon=True)
|
||||
|
||||
Reference in New Issue
Block a user