77 lines
3.2 KiB
Plaintext
77 lines
3.2 KiB
Plaintext
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from datetime import datetime
|
|
import os
|
|
|
|
db = SQLAlchemy()
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
|
|
# ========================================================================
|
|
# CONFIGURATION - Environment-based for Docker compatibility
|
|
# ========================================================================
|
|
|
|
# Secret key for session management
|
|
# CRITICAL: Set SECRET_KEY environment variable in production!
|
|
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'your_secret_key_change_in_production')
|
|
|
|
# Database configuration - supports both SQLite (legacy) and MariaDB (Docker)
|
|
database_type = os.getenv('DATABASE_TYPE', 'mariadb') # 'sqlite' or 'mariadb'
|
|
|
|
if database_type == 'sqlite':
|
|
# SQLite mode (legacy/development)
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
|
|
app.logger.warning('Using SQLite database - not recommended for production!')
|
|
else:
|
|
# MariaDB mode (Docker/production) - recommended
|
|
db_user = os.getenv('DB_USER', 'trasabilitate')
|
|
db_password = os.getenv('DB_PASSWORD', 'Initial01!')
|
|
db_host = os.getenv('DB_HOST', 'localhost')
|
|
db_port = os.getenv('DB_PORT', '3306')
|
|
db_name = os.getenv('DB_NAME', 'trasabilitate')
|
|
|
|
# Construct MariaDB connection string
|
|
# Format: mysql+mariadb://user:password@host:port/database
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = (
|
|
f'mysql+mariadb://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}'
|
|
)
|
|
app.logger.info(f'Using MariaDB database: {db_user}@{db_host}:{db_port}/{db_name}')
|
|
|
|
# Disable SQLAlchemy modification tracking (improves performance)
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
# Connection pool settings for MariaDB
|
|
if database_type == 'mariadb':
|
|
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
|
'pool_size': int(os.getenv('DB_POOL_SIZE', '10')),
|
|
'pool_recycle': int(os.getenv('DB_POOL_RECYCLE', '3600')), # Recycle connections after 1 hour
|
|
'pool_pre_ping': True, # Verify connections before using
|
|
'max_overflow': int(os.getenv('DB_MAX_OVERFLOW', '20')),
|
|
'echo': os.getenv('SQLALCHEMY_ECHO', 'false').lower() == 'true' # SQL query logging
|
|
}
|
|
|
|
# Initialize SQLAlchemy with app
|
|
db.init_app(app)
|
|
|
|
# Register blueprints
|
|
from app.routes import bp as main_bp, warehouse_bp
|
|
app.register_blueprint(main_bp, url_prefix='/')
|
|
app.register_blueprint(warehouse_bp)
|
|
|
|
# Add 'now' function to Jinja2 globals for templates
|
|
app.jinja_env.globals['now'] = datetime.now
|
|
|
|
# Create database tables if they don't exist
|
|
# Note: In Docker, schema is created by setup_complete_database.py
|
|
# This is kept for backwards compatibility
|
|
with app.app_context():
|
|
try:
|
|
db.create_all()
|
|
app.logger.info('Database tables verified/created')
|
|
except Exception as e:
|
|
app.logger.error(f'Error creating database tables: {e}')
|
|
# Don't fail startup if tables already exist or schema is managed externally
|
|
|
|
return app
|