86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
"""
|
|
SKE Digital Signage Server Application Factory
|
|
"""
|
|
|
|
import os
|
|
from flask import Flask
|
|
from flask_migrate import Migrate
|
|
|
|
def create_app(config_name=None):
|
|
"""Application factory function"""
|
|
|
|
# Create Flask application
|
|
app = Flask(__name__, instance_relative_config=True)
|
|
|
|
# Load configuration
|
|
if config_name is None:
|
|
config_name = os.environ.get('FLASK_CONFIG', 'default')
|
|
|
|
from config import config
|
|
app.config.from_object(config[config_name])
|
|
|
|
# Ensure instance folder exists
|
|
os.makedirs(app.instance_path, exist_ok=True)
|
|
|
|
# Initialize extensions
|
|
from app.extensions import db, bcrypt, login_manager
|
|
|
|
db.init_app(app)
|
|
bcrypt.init_app(app)
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'auth.login'
|
|
login_manager.login_message = 'Please log in to access this page.'
|
|
|
|
# Initialize Flask-Migrate
|
|
migrate = Migrate(app, db)
|
|
|
|
# Register user loader for Flask-Login
|
|
from app.models.user import User
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return db.session.get(User, int(user_id))
|
|
|
|
# Register blueprints
|
|
from app.routes.auth import bp as auth_bp
|
|
from app.routes.dashboard import bp as dashboard_bp
|
|
from app.routes.admin import bp as admin_bp
|
|
from app.routes.player import bp as player_bp
|
|
from app.routes.group import bp as group_bp
|
|
from app.routes.content import bp as content_bp
|
|
from app.routes.api import bp as api_bp
|
|
|
|
app.register_blueprint(auth_bp, url_prefix='/auth')
|
|
app.register_blueprint(dashboard_bp)
|
|
app.register_blueprint(admin_bp, url_prefix='/admin')
|
|
app.register_blueprint(player_bp, url_prefix='/player')
|
|
app.register_blueprint(group_bp, url_prefix='/group')
|
|
app.register_blueprint(content_bp, url_prefix='/content')
|
|
app.register_blueprint(api_bp, url_prefix='/api')
|
|
|
|
# Create upload folders
|
|
upload_path = os.path.join(app.static_folder, 'uploads')
|
|
assets_path = os.path.join(app.static_folder, 'assets')
|
|
os.makedirs(upload_path, exist_ok=True)
|
|
os.makedirs(assets_path, exist_ok=True)
|
|
|
|
# Context processor for theme injection
|
|
@app.context_processor
|
|
def inject_theme():
|
|
from flask_login import current_user
|
|
if current_user.is_authenticated:
|
|
theme = getattr(current_user, 'theme', 'light')
|
|
else:
|
|
theme = 'light'
|
|
return dict(theme=theme)
|
|
|
|
# Context processor for server info
|
|
@app.context_processor
|
|
def inject_server_info():
|
|
return dict(
|
|
server_version=app.config['SERVER_VERSION'],
|
|
build_date=app.config['BUILD_DATE']
|
|
)
|
|
|
|
return app
|