feat(app): refactor to app-factory; add application package and routes
This commit is contained in:
43
application/__init__.py
Normal file
43
application/__init__.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
from flask import Flask
|
||||
from .extensions import db, migrate
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
|
||||
def create_app(config=None):
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
# point templates/static to repository-level folders for compatibility
|
||||
project_root = os.path.dirname(BASE_DIR)
|
||||
app = Flask(__name__, template_folder=os.path.join(project_root, 'templates'), static_folder=os.path.join(project_root, 'static'), instance_relative_config=True)
|
||||
# default config
|
||||
DB_PATH = os.path.join(os.path.dirname(BASE_DIR), 'anmeldung.db')
|
||||
app.config.setdefault('SQLALCHEMY_DATABASE_URI', f'sqlite:///{DB_PATH}')
|
||||
app.config.setdefault('SQLALCHEMY_TRACK_MODIFICATIONS', False)
|
||||
|
||||
if config:
|
||||
app.config.update(config)
|
||||
|
||||
# init extensions
|
||||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
|
||||
# logging setup (mirror previous behavior)
|
||||
LOG_DIR = os.path.join(os.path.dirname(BASE_DIR), 'logs')
|
||||
os.makedirs(LOG_DIR, exist_ok=True)
|
||||
log_file = os.path.join(LOG_DIR, 'app.log')
|
||||
handler = RotatingFileHandler(log_file, maxBytes=5 * 1024 * 1024, backupCount=3)
|
||||
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s: %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
handler.setLevel(logging.INFO)
|
||||
app.logger.addHandler(handler)
|
||||
app.logger.setLevel(logging.INFO)
|
||||
|
||||
# register blueprints
|
||||
from .routes import bp as public_bp
|
||||
app.register_blueprint(public_bp)
|
||||
|
||||
# expose BASE_DIR on app for compatibility
|
||||
app.BASE_DIR = os.path.dirname(BASE_DIR)
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user