88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
import os
|
||
from flask import Flask, redirect, url_for, request, current_app
|
||
from flask_sqlalchemy import SQLAlchemy
|
||
from flask_login import LoginManager, current_user
|
||
from flask_migrate import Migrate
|
||
from sqlalchemy import text
|
||
|
||
# ✅ Docker-Pfade
|
||
UPLOAD_FOLDER = '/app/uploads'
|
||
RESULT_FOLDER = '/app/results'
|
||
|
||
db = SQLAlchemy()
|
||
login_manager = LoginManager()
|
||
migrate = Migrate()
|
||
|
||
|
||
def _run_migrations(app):
|
||
"""Fehlende DB-Spalten automatisch hinzufügen – übersteht jeden Neustart"""
|
||
migrations = [
|
||
("job", "result_filename_raw", "VARCHAR(150)"),
|
||
("job", "scraper_job_id", "VARCHAR(255)"),
|
||
("user", "is_admin", "BOOLEAN DEFAULT 0"),
|
||
]
|
||
with app.app_context():
|
||
for table, column, col_type in migrations:
|
||
try:
|
||
db.session.execute(text(f"ALTER TABLE {table} ADD COLUMN {column} {col_type}"))
|
||
db.session.commit()
|
||
print(f"✅ Migration: {table}.{column} hinzugefügt")
|
||
except Exception:
|
||
db.session.rollback()
|
||
|
||
|
||
def create_app():
|
||
app = Flask(__name__)
|
||
|
||
# 🔑 Configs
|
||
app.config['SECRET_KEY'] = '008e7369b075886d5f494c8813efdfb17155da6af12b3fe8ee'
|
||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
|
||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||
app.config['RESULT_FOLDER'] = RESULT_FOLDER
|
||
app.config['ALLOW_USER_SIGNUP'] = False
|
||
|
||
# DB + Tools
|
||
db.init_app(app)
|
||
migrate.init_app(app, db)
|
||
login_manager.init_app(app)
|
||
login_manager.login_view = 'auth.login'
|
||
|
||
# User Loader
|
||
@login_manager.user_loader
|
||
def load_user(user_id):
|
||
from .models import User
|
||
return User.query.get(int(user_id))
|
||
|
||
# Protected Routes
|
||
@app.before_request
|
||
def require_login():
|
||
allowed = ['auth.login', 'auth.signup', 'static']
|
||
if (not current_user.is_authenticated and
|
||
request.endpoint not in allowed and
|
||
not request.path.startswith('/static')):
|
||
return redirect(url_for('auth.login'))
|
||
|
||
# Ordner
|
||
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
||
os.makedirs(app.config['RESULT_FOLDER'], exist_ok=True)
|
||
|
||
# Routes
|
||
from . import routes
|
||
app.register_blueprint(routes.bp)
|
||
|
||
# Index Redirect
|
||
@app.route('/')
|
||
def index():
|
||
return redirect(url_for('auth.job_status'))
|
||
|
||
# DB Tables + Auto-Migration
|
||
with app.app_context():
|
||
db.create_all()
|
||
_run_migrations(app)
|
||
|
||
return app
|
||
|
||
if __name__ == '__main__':
|
||
app = create_app()
|
||
app.run(host='0.0.0.0', port=5000, debug=False)
|