From f4bc5543960a16cb2e9bc3be0de85d51743519b6 Mon Sep 17 00:00:00 2001 From: VivianDee <115420678+VivianDee@users.noreply.github.com> Date: Wed, 15 Oct 2025 11:21:53 +0100 Subject: [PATCH] [add]: health endpoint --- app/api/routes/routes.py | 46 ++++++++++++++++++++++++++++++++- app/swagger/digifi_swagger.json | 37 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/app/api/routes/routes.py b/app/api/routes/routes.py index 0f599c0..7f10dce 100644 --- a/app/api/routes/routes.py +++ b/app/api/routes/routes.py @@ -23,6 +23,9 @@ from flask_jwt_extended import ( get_jwt_identity, create_refresh_token, ) +from sqlalchemy import text +from app.extensions import db +from app.config import settings api = Blueprint('api', __name__) @@ -266,4 +269,45 @@ def get_all_offers(): # } # # logger.info(f"Get charges request received with filters: {filters}") # response = ChargeService.get_all_charges(filters) -# return jsonify(response) \ No newline at end of file +# return jsonify(response) + + +# Health Check Endpoint +@api.route("/health", methods=["GET"]) +def health_check(): + SQLALCHEMY_DATABASE_URI = settings.SQLALCHEMY_DATABASE_URI + response = {} + db_status = "Connection Successful" + errors = [] + status = "ok" + + + # Extract the database URI + try: + db_uri = db.engine.url.render_as_string(hide_password=False) + db_uri = db_uri + except Exception as e: + db_uri = "Unavailable" + errors.append(f"Database URI Error: {str(e)}") + + + + # Check database connection + try: + logger.info(f"Database Health == : {SQLALCHEMY_DATABASE_URI}") + db.session.execute(text("SELECT 1")) + except Exception as e: + db_status = "Connection Failed" + errors.append(f"Database Error: {str(e)}") + status = "failed" + + response = { + "status": status, + "db_status": db_status, + "db_uri": db_uri, + "errors": errors or None + } + + + return jsonify(response), 200 if status == "ok" else 500 + diff --git a/app/swagger/digifi_swagger.json b/app/swagger/digifi_swagger.json index 82befee..e478ee5 100644 --- a/app/swagger/digifi_swagger.json +++ b/app/swagger/digifi_swagger.json @@ -103,6 +103,10 @@ "description": "Find out more", "url": "https://www.simbrellang.net" } + }, + { + "name": "Health", + "description": "System health check including DB status." } ], "paths": { @@ -138,6 +142,39 @@ }, "/transaction-offers": { "$ref": "../swagger/paths/TransactionOffers.json" + }, + "/health": { + "get": { + "tags": ["Health"], + "summary": "Health Check", + "description": "Returns service health information including DB connection status.", + "responses": { + "200": { + "description": "Health check successful", + "content": { + "application/json": { + "example": { + "status": "ok", + "db_status": "Connection Successful", + "error": [] + } + } + } + }, + "500": { + "description": "Health check failed", + "content": { + "application/json": { + "example": { + "status": "ok", + "db_status": "Connection Failed", + "error":["could not connect to server: Connection refused"] + } + } + } + } + } + } } }, "components": {