Add health proxy service and Nginx configuration for health checks
This commit is contained in:
34
backend/health-proxy.py
Normal file
34
backend/health-proxy.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from flask import Flask, request, jsonify, abort
|
||||
import requests
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Optional token for access control (not production secure)
|
||||
HEALTH_TOKEN = os.environ.get('HEALTH_TOKEN')
|
||||
|
||||
@app.route('/healthcheck')
|
||||
def healthcheck():
|
||||
token = request.args.get('token') or request.headers.get('X-Health-Token')
|
||||
if HEALTH_TOKEN and token != HEALTH_TOKEN:
|
||||
return jsonify({'error': 'Unauthorized'}), 401
|
||||
|
||||
host = request.args.get('host')
|
||||
port = request.args.get('port')
|
||||
proto = request.args.get('proto', 'http')
|
||||
if not host or not port:
|
||||
return jsonify({'error': 'missing parameters, expected host and port'}), 400
|
||||
|
||||
url = f"{proto}://{host}:{port}"
|
||||
|
||||
try:
|
||||
# Use HEAD to do a lightweight check
|
||||
r = requests.head(url, timeout=4, allow_redirects=True, verify=False)
|
||||
# Any 2xx/3xx is considered OK
|
||||
ok = 200 <= r.status_code < 400
|
||||
return jsonify({'ok': ok, 'status_code': r.status_code}), (200 if ok else 502)
|
||||
except requests.RequestException as e:
|
||||
return jsonify({'ok': False, 'error': str(e)}), 502
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=8081)
|
||||
Reference in New Issue
Block a user