# -*- coding: utf-8 -*- """ This blueprint contains common reusable pieces of functionality of the application. """ from http import HTTPStatus from flask import Blueprint, jsonify from auth import util as auth_util from core.clients import clients from core.constants import Service from core.exceptions import APIError from core.serializers import HealthCheckParams from main_db import util as db_util from redis_db import redis_client blueprint = Blueprint("core", __name__) @blueprint.app_errorhandler(APIError) def handle_api_error(error): return error.respond() @blueprint.route("/hello", methods=["GET"]) @auth_util.no_authorize def basic_health_check(): """Check health of the application.""" return jsonify({"status": "ok"}) @blueprint.route("/health", methods=["GET"]) @auth_util.no_authorize def full_health_check(): """Check health of the application and all the services it depends on.""" data = HealthCheckParams().load_from_request() include = [i.strip().lower() for i in data["include"]] points_to_check = { Service.AUTH0: auth_util.check_health, Service.DSP_API: clients.dsp.check_health, Service.VENDOR_API: clients.vendor.check_health, Service.CONSUMER_ANALYTICS_API: clients.ca.check_health, Service.DB: db_util.check_health, Service.REDIS: redis_client.ping, } errors = [] for service_name, check_func in points_to_check.items(): if service_name not in include and Service.ALL not in include: continue try: result = check_func() if not result: errors.append(dict(name=service_name)) except Exception as ex: errors.append(dict(name=service_name, details=str(ex))) if errors: return jsonify(errors), HTTPStatus.BAD_GATEWAY return jsonify({"status": "ok"})