v1
This commit is contained in:
56
server/factory.py
Normal file
56
server/factory.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""Application factory for the Flask server."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from . import logging_config, middleware, routes, settings
|
||||
from .database import init_db, is_postgres_enabled
|
||||
|
||||
|
||||
def _configure_sentry() -> None:
|
||||
if not settings.SENTRY_DSN:
|
||||
return
|
||||
try:
|
||||
import sentry_sdk
|
||||
from sentry_sdk.integrations.flask import FlaskIntegration
|
||||
|
||||
sentry_sdk.init(
|
||||
dsn=settings.SENTRY_DSN,
|
||||
integrations=[FlaskIntegration()],
|
||||
traces_sample_rate=settings.SENTRY_TRACES_SAMPLE_RATE,
|
||||
)
|
||||
logging.info("Sentry initialized")
|
||||
except Exception:
|
||||
logging.exception("Failed to initialize Sentry SDK")
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
"""Create and configure the Flask application instance."""
|
||||
logging_config.configure_logging()
|
||||
|
||||
if settings.POSTGRES_URL:
|
||||
try:
|
||||
import psycopg2 # type: ignore # noqa: F401
|
||||
except Exception:
|
||||
logging.warning(
|
||||
"POSTGRES_URL is set but psycopg2 is not installed; falling back to SQLite"
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_mapping(SECRET_KEY=settings.SECRET_KEY)
|
||||
app.template_folder = str(settings.BASE_DIR / "templates")
|
||||
|
||||
middleware.register_request_hooks(app)
|
||||
routes.register_blueprints(app)
|
||||
|
||||
try:
|
||||
init_db()
|
||||
except Exception:
|
||||
logging.exception("Failed to initialize DB at import time")
|
||||
|
||||
is_postgres_enabled()
|
||||
_configure_sentry()
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user