97 lines
3.8 KiB
Python
97 lines
3.8 KiB
Python
import datetime as _dt
|
|
import os
|
|
from decimal import Decimal
|
|
|
|
from flask import Flask, jsonify
|
|
from flask.json.provider import DefaultJSONProvider
|
|
|
|
from webapi.blueprints.cacu import cacu_bp
|
|
from webapi.blueprints.data import data_bp
|
|
from webapi.blueprints.governance import governance_bp
|
|
from webapi.blueprints.imports import imports_bp
|
|
from webapi.blueprints.reports import reports_bp
|
|
from webapi.follow_up.routes import follow_up_blueprint
|
|
|
|
|
|
class IsoJSONProvider(DefaultJSONProvider):
|
|
"""ISO-8601 dates (Flask's default is RFC 822); files are never sent
|
|
inline in JSON, only their size."""
|
|
|
|
@staticmethod
|
|
def default(o):
|
|
if isinstance(o, (_dt.datetime, _dt.date, _dt.time)):
|
|
return o.isoformat()
|
|
if isinstance(o, Decimal):
|
|
return float(o)
|
|
if isinstance(o, (bytes, bytearray)):
|
|
return f"<{len(o)} bytes>"
|
|
return DefaultJSONProvider.default(o)
|
|
|
|
|
|
def create_app() -> Flask:
|
|
app = Flask(__name__)
|
|
app.json = IsoJSONProvider(app)
|
|
# File sources travel inside the request; cap them per request.
|
|
app.config["MAX_CONTENT_LENGTH"] = int(os.environ.get("WEBAPI_MAX_UPLOAD_MB", "200")) * 1024 * 1024
|
|
|
|
# The web client goes through a proxy; browser clients on other origins
|
|
# are allowed with WEBAPI_CORS_ORIGINS (comma-separated).
|
|
cors_origins = os.environ.get("WEBAPI_CORS_ORIGINS")
|
|
if cors_origins:
|
|
from flask_cors import CORS
|
|
|
|
CORS(app, origins=[origin.strip() for origin in cors_origins.split(",")])
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|
|
|
|
@app.get("/api")
|
|
def api_index():
|
|
"""Every route with its methods and summary, for programmatic clients."""
|
|
routes = []
|
|
for rule in sorted(app.url_map.iter_rules(), key=lambda r: r.rule):
|
|
if rule.endpoint == "static":
|
|
continue
|
|
doc = (app.view_functions[rule.endpoint].__doc__ or "").strip()
|
|
routes.append(
|
|
{
|
|
"path": rule.rule,
|
|
"methods": sorted(rule.methods - {"HEAD", "OPTIONS"}),
|
|
"summary": " ".join(doc.split("\n\n")[0].split()),
|
|
}
|
|
)
|
|
return jsonify({"routes": routes})
|
|
|
|
@app.errorhandler(413)
|
|
def too_large(_exc):
|
|
limit_mb = app.config["MAX_CONTENT_LENGTH"] // (1024 * 1024)
|
|
return jsonify({"error": f"la solicitud excede {limit_mb} MB"}), 413
|
|
|
|
app.register_blueprint(cacu_bp, url_prefix="/cacu")
|
|
app.register_blueprint(imports_bp, url_prefix="/imports")
|
|
app.register_blueprint(data_bp, url_prefix="/data")
|
|
app.register_blueprint(governance_bp, url_prefix="/governance")
|
|
app.register_blueprint(reports_bp, url_prefix="/reportes")
|
|
# The measures, answered on request from dbt's semantic definitions: the deployment ships their manifest.
|
|
if os.environ.get("DBT_MANIFEST"):
|
|
from importlib import import_module
|
|
from pathlib import Path
|
|
|
|
from webapi.kpis import kpis_blueprint
|
|
|
|
for domain in ("cacu", "cama"):
|
|
app.register_blueprint(kpis_blueprint(domain, import_module(f"persistance.{domain}"),
|
|
Path(os.environ["DBT_MANIFEST"])), url_prefix=f"/{domain}")
|
|
# Patient-level follow-up list and individual risk, marked `simulado` in every response. Who may see them is
|
|
# an access rule still to be defined, not an environment variable.
|
|
from persistance import cacu as cacu_repositories
|
|
from persistance.db import get_cacu_session
|
|
|
|
from webapi.patient_risk import patient_risk_blueprint
|
|
|
|
app.register_blueprint(follow_up_blueprint(get_cacu_session), url_prefix="/cacu")
|
|
app.register_blueprint(patient_risk_blueprint(cacu_repositories), url_prefix="/cacu")
|
|
|
|
return app
|