elemes/app.py

56 lines
1.7 KiB
Python
Raw Normal View History

2026-01-02 06:18:48 +07:00
#!/usr/bin/env python3
"""
C Programming Learning Management System
2026-03-25 09:39:51 +07:00
Application factory assembles blueprints and startup tasks.
Flask serves as a JSON API consumed by the SvelteKit frontend.
2026-01-02 06:18:48 +07:00
"""
2026-01-12 12:03:23 +07:00
import logging
2026-01-02 06:18:48 +07:00
2026-03-25 09:39:51 +07:00
from flask import Flask
from flask_cors import CORS
2026-01-02 06:18:48 +07:00
2026-03-25 09:39:51 +07:00
from services.lesson_service import get_lesson_names
from services.token_service import initialize_tokens_file
2026-01-12 12:03:23 +07:00
2026-03-25 09:39:51 +07:00
# Configure logging once at module level
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
2026-01-06 21:38:54 +07:00
)
2026-01-02 06:18:48 +07:00
2026-01-17 18:58:16 +07:00
2026-03-25 09:39:51 +07:00
def create_app():
"""Application factory."""
app = Flask(__name__)
2026-01-17 18:58:16 +07:00
2026-03-25 09:39:51 +07:00
# Allow cross-origin requests from the SvelteKit frontend
CORS(app)
2026-01-17 18:58:16 +07:00
2026-03-25 09:39:51 +07:00
# ── Blueprints ────────────────────────────────────────────────────
from routes.auth import auth_bp
from routes.compile import compile_bp
from routes.lessons import lessons_bp
from routes.progress import progress_bp
from routes.help import help_bp
2026-01-17 18:58:16 +07:00
2026-03-25 09:39:51 +07:00
app.register_blueprint(auth_bp)
app.register_blueprint(compile_bp)
app.register_blueprint(lessons_bp)
app.register_blueprint(progress_bp)
app.register_blueprint(help_bp)
2026-01-17 18:58:16 +07:00
2026-03-25 09:39:51 +07:00
# ── Startup tasks ─────────────────────────────────────────────────
initialize_tokens_file(get_lesson_names())
2026-01-02 08:45:32 +07:00
2026-03-25 09:39:51 +07:00
return app
2026-01-02 06:18:48 +07:00
2026-01-02 08:45:32 +07:00
2026-03-25 09:39:51 +07:00
# Gunicorn entry: gunicorn "app:create_app()"
# Dev entry: python app.py
2026-01-02 06:18:48 +07:00
if __name__ == '__main__':
2026-01-12 12:03:23 +07:00
import os
debug_mode = os.environ.get('FLASK_DEBUG', 'False').lower() == 'true'
2026-03-25 09:39:51 +07:00
application = create_app()
application.run(host='0.0.0.0', port=5000, debug=debug_mode)