Add whitelist health check
This commit is contained in:
+35
@@ -2,9 +2,14 @@
|
||||
import os
|
||||
|
||||
from flask import Flask, jsonify
|
||||
import requests
|
||||
|
||||
app = Flask(__name__)
|
||||
VERSION = "0.1.0"
|
||||
WHITELIST_URL = os.getenv(
|
||||
"WHITELIST_URL",
|
||||
"https://whitelist.nodejsk8s.services.ngcloud.ru/v2/app",
|
||||
)
|
||||
|
||||
|
||||
def mariadb_configuration():
|
||||
@@ -18,6 +23,27 @@ def mariadb_configuration():
|
||||
}
|
||||
|
||||
|
||||
def check_whitelist():
|
||||
headers = {}
|
||||
token = os.getenv("WHITELIST_TOKEN")
|
||||
if token:
|
||||
headers["Authorization"] = f"Bearer {token}"
|
||||
response = requests.get(
|
||||
WHITELIST_URL,
|
||||
headers=headers,
|
||||
allow_redirects=False,
|
||||
timeout=int(os.getenv("WHITELIST_TIMEOUT", "10")),
|
||||
)
|
||||
content_type = response.headers.get("Content-Type", "")
|
||||
body = response.json() if "json" in content_type else response.text
|
||||
return {
|
||||
"url": WHITELIST_URL,
|
||||
"status": response.status_code,
|
||||
"location": response.headers.get("Location"),
|
||||
"body": body,
|
||||
}
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def index():
|
||||
return jsonify({
|
||||
@@ -34,6 +60,15 @@ def health():
|
||||
return jsonify({"ok": True, "version": VERSION})
|
||||
|
||||
|
||||
@app.get("/check")
|
||||
def check():
|
||||
try:
|
||||
result = check_whitelist()
|
||||
return jsonify({"ok": 200 <= result["status"] < 300, **result})
|
||||
except requests.RequestException as exc:
|
||||
return jsonify({"ok": False, "url": WHITELIST_URL, "error": str(exc)}), 502
|
||||
|
||||
|
||||
@app.get("/mariadb")
|
||||
def mariadb():
|
||||
return jsonify(mariadb_configuration())
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
flask
|
||||
flask
|
||||
requests
|
||||
Reference in New Issue
Block a user