62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
import os
|
|
from flask import Flask
|
|
from flask_migrate import Migrate
|
|
from flask_socketio import SocketIO
|
|
from dotenv import load_dotenv
|
|
from sqlalchemy import text
|
|
from .models import db
|
|
|
|
socketio = SocketIO(cors_allowed_origins="*")
|
|
migrate = Migrate()
|
|
|
|
|
|
def create_app():
|
|
load_dotenv()
|
|
|
|
app = Flask(__name__)
|
|
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "dev-secret")
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
|
|
"DATABASE_URL",
|
|
"postgresql+psycopg2://rezept_finder:rezept_finder@localhost:5432/rezept_finder",
|
|
)
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
@app.after_request
|
|
def add_cors_headers(response):
|
|
origin = os.getenv("FRONTEND_ORIGIN", "*")
|
|
response.headers["Access-Control-Allow-Origin"] = origin
|
|
response.headers["Access-Control-Allow-Headers"] = "Content-Type"
|
|
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
|
|
return response
|
|
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
socketio.init_app(app, cors_allowed_origins=os.getenv("FRONTEND_ORIGIN", "*"))
|
|
|
|
from .routes.sessions import bp as sessions_bp
|
|
app.register_blueprint(sessions_bp, url_prefix="/api/sessions")
|
|
|
|
from .socket import events # noqa: F401
|
|
|
|
with app.app_context():
|
|
ensure_schema()
|
|
|
|
return app
|
|
|
|
|
|
def ensure_schema():
|
|
db.create_all()
|
|
engine_name = db.engine.url.get_backend_name()
|
|
if engine_name != "postgresql":
|
|
return
|
|
|
|
db.session.execute(text(
|
|
"ALTER TABLE session_settings "
|
|
"ADD COLUMN IF NOT EXISTS match_mode VARCHAR(20) NOT NULL DEFAULT 'first_match'"
|
|
))
|
|
db.session.execute(text(
|
|
"ALTER TABLE session_settings "
|
|
"ADD COLUMN IF NOT EXISTS voting_round INTEGER NOT NULL DEFAULT 1"
|
|
))
|
|
db.session.commit()
|