117 lines
4.4 KiB
Python
117 lines
4.4 KiB
Python
import os
|
|
|
|
from dotenv import load_dotenv
|
|
from flask import Flask
|
|
from flask_cors import CORS
|
|
from flask_limiter import Limiter
|
|
from flask_limiter.util import get_remote_address
|
|
from flask_migrate import Migrate
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from sqlalchemy import MetaData
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
from logging_config import configure_logging
|
|
|
|
environment = os.environ.get("APP_ENV", "development").lower()
|
|
if environment == "development":
|
|
load_dotenv()
|
|
environment = os.environ.get("APP_ENV", environment).lower()
|
|
if environment not in {"development", "testing", "production"}:
|
|
raise RuntimeError(
|
|
"APP_ENV must be one of: development, testing, production"
|
|
)
|
|
|
|
|
|
def env_bool(name, default=False):
|
|
value = os.environ.get(name)
|
|
if value is None:
|
|
return default
|
|
return value.lower() in {"1", "true", "yes", "on"}
|
|
|
|
convention = {
|
|
"ix": "ix_%(column_0_label)s",
|
|
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
|
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
|
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
|
"pk": "pk_%(table_name)s",
|
|
}
|
|
|
|
app = Flask(__name__)
|
|
app.config.update(
|
|
{
|
|
"APP_ENV": environment,
|
|
"DEBUG": env_bool("DEBUG", environment == "development"),
|
|
"TESTING": environment == "testing",
|
|
"SECRET_KEY": os.environ.get("SECRET_KEY"),
|
|
"SQLALCHEMY_DATABASE_URI": os.environ.get(
|
|
"SQLALCHEMY_DATABASE_URI",
|
|
"sqlite:///:memory:"
|
|
if environment == "testing"
|
|
else "sqlite:///mydatabase.sqlite",
|
|
),
|
|
"SQLALCHEMY_ECHO": env_bool("SQLALCHEMY_ECHO"),
|
|
"SQLALCHEMY_TRACK_MODIFICATIONS": env_bool(
|
|
"SQLALCHEMY_TRACK_MODIFICATIONS"
|
|
),
|
|
"APP_URL": os.environ.get("APP_URL", "http://localhost:8080").rstrip("/"),
|
|
"CORS_ORIGINS": [
|
|
origin.strip()
|
|
for origin in os.environ.get("CORS_ORIGINS", "*").split(",")
|
|
if origin.strip()
|
|
],
|
|
"REGISTRATION_MODE": os.environ.get("REGISTRATION_MODE", "open").lower(),
|
|
"EMAIL_DELIVERY_ENABLED": env_bool(
|
|
"EMAIL_DELIVERY_ENABLED", environment == "production"
|
|
),
|
|
"RESEND_API_KEY": os.environ.get("RESEND_API_KEY"),
|
|
"EMAIL_FROM": os.environ.get("EMAIL_FROM"),
|
|
"CONTACT_EMAIL": os.environ.get("CONTACT_EMAIL"),
|
|
"LOG_LEVEL": os.environ.get("LOG_LEVEL", "INFO").upper(),
|
|
"MAX_CONTENT_LENGTH": int(os.environ.get("MAX_CONTENT_LENGTH", 1_048_576)),
|
|
"RATELIMIT_ENABLED": env_bool("RATELIMIT_ENABLED", True),
|
|
"SESSION_COOKIE_SECURE": env_bool(
|
|
"SESSION_COOKIE_SECURE", environment == "production"
|
|
),
|
|
"SESSION_COOKIE_HTTPONLY": env_bool("SESSION_COOKIE_HTTPONLY", True),
|
|
"SESSION_COOKIE_SAMESITE": os.environ.get("SESSION_COOKIE_SAMESITE", "Lax"),
|
|
}
|
|
)
|
|
|
|
if app.config["REGISTRATION_MODE"] not in {"open", "closed"}:
|
|
raise RuntimeError("REGISTRATION_MODE must be one of: open, closed")
|
|
if environment == "production":
|
|
required = ["SECRET_KEY", "SQLALCHEMY_DATABASE_URI", "CORS_ORIGINS"]
|
|
if app.config["EMAIL_DELIVERY_ENABLED"]:
|
|
required.extend(
|
|
["RESEND_API_KEY", "EMAIL_FROM", "CONTACT_EMAIL", "APP_URL"]
|
|
)
|
|
missing = [name for name in required if not os.environ.get(name)]
|
|
if missing:
|
|
raise RuntimeError(
|
|
"Missing required production configuration: " + ", ".join(missing)
|
|
)
|
|
if len(app.config["SECRET_KEY"]) < 32:
|
|
raise RuntimeError("SECRET_KEY must contain at least 32 characters in production")
|
|
if "*" in app.config["CORS_ORIGINS"]:
|
|
raise RuntimeError("CORS_ORIGINS must not contain '*' in production")
|
|
if app.config["SESSION_COOKIE_SAMESITE"].lower() == "none" and not app.config[
|
|
"SESSION_COOKIE_SECURE"
|
|
]:
|
|
raise RuntimeError("SESSION_COOKIE_SAMESITE=None requires SESSION_COOKIE_SECURE=true")
|
|
if app.config["SQLALCHEMY_ECHO"]:
|
|
raise RuntimeError("SQLALCHEMY_ECHO must be disabled in production")
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
|
|
configure_logging(app)
|
|
|
|
metadata = MetaData(naming_convention=convention)
|
|
db = SQLAlchemy(app, metadata=metadata)
|
|
migrate = Migrate(app, db, render_as_batch=True)
|
|
CORS(app, origins=app.config["CORS_ORIGINS"], supports_credentials=True)
|
|
limiter = Limiter(
|
|
get_remote_address,
|
|
app=app,
|
|
enabled=app.config["RATELIMIT_ENABLED"],
|
|
storage_uri="memory://",
|
|
)
|