57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
"""Flask application factory."""
|
|
import os
|
|
from flask import Flask
|
|
from flask_cors import CORS
|
|
from app.extensions import db
|
|
from app.routes_tunes import tunes_bp
|
|
from app.routes_instruments import instruments_bp
|
|
from app.routes_sub import sub_bp
|
|
from app.routes_sources import sources_bp
|
|
from app.routes_musicians import musicians_bp
|
|
from app.routes_tunings import tunings_bp
|
|
from app.routes_reference_sites import reference_sites_bp
|
|
from app.routes_terms import terms_bp
|
|
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
|
|
# --- Config ---
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ["DATABASE_URL"]
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
# --- CORS ---
|
|
# CORS_ORIGINS in .env is a comma-separated list of allowed origins.
|
|
# Defaults to * for local dev. Lock this down in production to your
|
|
# S3 bucket / CloudFront domain, e.g.:
|
|
# CORS_ORIGINS=https://yourdomain.com,https://www.yourdomain.com
|
|
raw = os.environ.get("CORS_ORIGINS", "*")
|
|
origins = [o.strip() for o in raw.split(",")] if raw != "*" else "*"
|
|
CORS(app, origins=origins, supports_credentials=False)
|
|
|
|
# --- Extensions ---
|
|
db.init_app(app)
|
|
|
|
# --- Blueprints ---
|
|
app.register_blueprint(tunes_bp)
|
|
app.register_blueprint(instruments_bp)
|
|
app.register_blueprint(sub_bp)
|
|
app.register_blueprint(sources_bp)
|
|
app.register_blueprint(musicians_bp)
|
|
app.register_blueprint(tunings_bp)
|
|
app.register_blueprint(reference_sites_bp)
|
|
app.register_blueprint(terms_bp)
|
|
|
|
# --- Error handlers ---
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
@app.errorhandler(IntegrityError)
|
|
def handle_integrity_error(e):
|
|
db.session.rollback()
|
|
return {"error": "Conflict — duplicate or constraint violation"}, 409
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|
|
|
|
return app
|