integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
+60
-1
@@ -1,19 +1,78 @@
|
||||
from flask import Flask
|
||||
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.cama import cama_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.tea import tea_bp
|
||||
|
||||
|
||||
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"request larger than {limit_mb} MB"}), 413
|
||||
|
||||
app.register_blueprint(cacu_bp, url_prefix="/cacu")
|
||||
app.register_blueprint(cama_bp, url_prefix="/cama")
|
||||
app.register_blueprint(tea_bp, url_prefix="/tea")
|
||||
app.register_blueprint(imports_bp, url_prefix="/imports")
|
||||
app.register_blueprint(data_bp, url_prefix="/data")
|
||||
app.register_blueprint(governance_bp, url_prefix="/governance")
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user