├── .gitignore ├── Dockerfile ├── app.py ├── requirements.txt └── templates └── hello.html /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .idea -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.6-alpine 2 | 3 | RUN pip install flask 4 | 5 | COPY . /opt/ 6 | 7 | EXPOSE 8080 8 | 9 | WORKDIR /opt 10 | 11 | ENTRYPOINT ["python", "app.py"] 12 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask import render_template 3 | import socket 4 | import random 5 | import os 6 | import argparse 7 | 8 | app = Flask(__name__) 9 | 10 | color_codes = { 11 | "red": "#e74c3c", 12 | "green": "#16a085", 13 | "blue": "#2980b9", 14 | "blue2": "#30336b", 15 | "pink": "#be2edd", 16 | "darkblue": "#130f40" 17 | } 18 | 19 | SUPPORTED_COLORS = ",".join(color_codes.keys()) 20 | 21 | # Get color from Environment variable 22 | COLOR_FROM_ENV = os.environ.get('APP_COLOR') 23 | # Generate a random color 24 | COLOR = random.choice(["red", "green", "blue", "blue2", "darkblue", "pink"]) 25 | 26 | 27 | @app.route("/") 28 | def main(): 29 | # return 'Hello' 30 | return render_template('hello.html', name=socket.gethostname(), color=color_codes[COLOR]) 31 | 32 | 33 | if __name__ == "__main__": 34 | 35 | print(" This is a sample web application that displays a colored background. \n" 36 | " A color can be specified in two ways. \n" 37 | "\n" 38 | " 1. As a command line argument with --color as the argument. Accepts one of " + SUPPORTED_COLORS + " \n" 39 | " 2. As an Environment variable APP_COLOR. Accepts one of " + SUPPORTED_COLORS + " \n" 40 | " 3. If none of the above then a random color is picked from the above list. \n" 41 | " Note: Command line argument precedes over environment variable.\n" 42 | "\n" 43 | "") 44 | 45 | # Check for Command Line Parameters for color 46 | parser = argparse.ArgumentParser() 47 | parser.add_argument('--color', required=False) 48 | args = parser.parse_args() 49 | 50 | if args.color: 51 | print("Color from command line argument =" + args.color) 52 | COLOR = args.color 53 | if COLOR_FROM_ENV: 54 | print("A color was set through environment variable -" + COLOR_FROM_ENV + ". However, color from command line argument takes precendence.") 55 | elif COLOR_FROM_ENV: 56 | print("No Command line argument. Color from environment variable =" + COLOR_FROM_ENV) 57 | COLOR = COLOR_FROM_ENV 58 | else: 59 | print("No command line argument or environment variable. Picking a Random Color =" + COLOR) 60 | 61 | # Check if input color is a supported one 62 | if COLOR not in color_codes: 63 | print("Color not supported. Received '" + COLOR + "' expected one of " + SUPPORTED_COLORS) 64 | exit(1) 65 | 66 | # Run Flask Application 67 | app.run(host="0.0.0.0", port=8080) 68 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask -------------------------------------------------------------------------------- /templates/hello.html: -------------------------------------------------------------------------------- 1 | 2 |