from flask import Flask, render_template, request import logging import os import requests app = Flask(__name__) app.logger.setLevel(logging.INFO) APP_VERSION = os.getenv('APP_VERSION') BIND_HOST = os.getenv('BIND_HOST') or '0.0.0.0' BIND_PORT = os.getenv('BIND_PORT') or '8000' @app.before_request def log_request(): app.logger.info( 'method=%s path=%s host=%s remote_addr=%s', request.method, request.path, request.host, request.remote_addr, ) @app.route('/') def home(): return f'Hello from app {APP_VERSION}' @app.route('/kittens') def kittens(): # Get a URL to a random kitten photo. response = requests.get('https://api.thecatapi.com/v1/images/search?api_') try: # Response structure is as follows: # [{"id":"bL3lrUi1A","url":"ex.com/bL3lrUi1A.jpg","width":1280,"height":720}] data = response.json() kitten_url = data[0]['url'] return render_template('index.html', kitten_url=kitten_url) except Exception as e: return f'Failed to fetch a kitten image :(\n {e}' if __name__ == '__main__': app.run(host=BIND_HOST, port=BIND_PORT)