Skip to content

Instantly share code, notes, and snippets.

@postcert
Created November 26, 2018 15:48
Show Gist options
  • Save postcert/49e9e8cb3b6074979f207dab749f5f40 to your computer and use it in GitHub Desktop.
Save postcert/49e9e8cb3b6074979f207dab749f5f40 to your computer and use it in GitHub Desktop.
simple python flask healthcheck toggler
from flask import Flask, jsonify
app = Flask(__name__)
health_status = True
@app.route('/toggle')
def toggle():
global health_status
health_status = not health_status
return jsonify(health_value=health_status)
@app.route('/health')
def health():
if health_status:
resp = jsonify(health="healthy")
resp.status_code = 200
else:
resp = jsonify(health="unhealthy")
resp.status_code = 500
return resp
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
@ankujuniyal
Copy link

we could reuse existing code
https://pypi.org/project/py-healthcheck/

`
from flask import Flask
from healthcheck import HealthCheck, EnvironmentDump

app = Flask(name)

health = HealthCheck()
envdump = EnvironmentDump()

add your own check function to the healthcheck

def redis_available():
client = _redis_client()
info = client.info()
return True, "redis ok"

health.add_check(redis_available)

add your own data to the environment dump

def application_data():
return {"maintainer": "Luis Fernando Gomes",
"git_repo": "https://github.com/ateliedocodigo/py-healthcheck"}

envdump.add_section("application", application_data)

Add a flask route to expose information

app.add_url_rule("/healthcheck", "healthcheck", view_func=lambda: health.run())
app.add_url_rule("/environment", "environment", view_func=lambda: envdump.run())
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment