Commit af28d550 authored by Simone Esposito's avatar Simone Esposito

frontend: Be more resilient to backend failures

parent 55ce886a
Pipeline #77486500 passed with stages
in 4 minutes
......@@ -11,34 +11,36 @@ app = Flask(__name__, template_folder='.')
def hello():
my_hostname = socket.gethostname()
# Let's fill this dictionary with the data from the API we need
api_data = {
key: get_api(key) for key in
('metadata', 'colors', 'configuration', 'state', 'redis_detection')
}
# Description to return on the index page
description = [
f'Hello there! This is the frontend container. My hostname is {my_hostname}',
'On the other hand, all this information comes from the backend:',
get_api('redis_detection')['description'],
get_api('metadata')['description'],
get_api('configuration')['description'],
api_data['redis_detection'].get('description', 'Unable to reach the backend.'),
api_data['metadata'].get('description', 'Unable to reach the backend.'),
api_data['configuration'].get('description', 'Unable to reach the backend.'),
]
# Transforming the list into an HTML-worthy text
description = '\n\n'.join(description)
# Let's render the template in `index.html` and return it.
# Each `get_api()` does an HTTP call to the backend.
# We do it separately so we call it only once
state = get_api('state')
return render_template(
'index.html',
description=description,
hostname=get_api('metadata')['hostname'],
hostname_color=get_api('colors')['hostname'],
version=get_api('configuration')['version'],
version_color=get_api('colors')['version'],
state_image_link=state['image_link'],
state_hits=state['state_hits'],
state_color=get_api('colors')['state'],
hostname=api_data['metadata'].get('hostname'),
hostname_color=api_data['colors'].get('hostname'),
version=api_data['configuration'].get('version'),
version_color=api_data['colors'].get('version'),
state_image_link=api_data['state'].get('image_link'),
state_hits=api_data['state'].get('state_hits'),
state_color=api_data['colors'].get('state'),
)
app.run(host='0.0.0.0', port=configuration.listen_port, debug=True)
......
......@@ -10,9 +10,12 @@ from configuration import backend_api_url
# Helper function so we don't have to rewrite the same logic everywhere
def get_api(path):
try:
r = requests.get(f'{backend_api_url}/api/v1/{path}', timeout=2) # Let's get that page!
r.raise_for_status() # In case we hit a 4xx-5xx response status, this will raise an exception.
return r.json() # We parse the JSON so we can readily use it
except:
return 'Something went wrong :( Here is the traceback:' + ''.join(traceback.format_stack())
try:
r = requests.get(f'{backend_api_url}/api/v1/{path}', timeout=2) # Let's get that page!
r.raise_for_status() # In case we hit a 4xx-5xx response status, this will raise an exception.
return r.json() # We parse the JSON so we can readily use it
except: # Something went wrong. We have nothing from the API.
print('We have not been able to reach the backend.')
print('This is the traceback:')
traceback.print_exc()
return {}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment