Skip to content

Instantly share code, notes, and snippets.

@ptrxyz
Last active April 11, 2022 01:04
Show Gist options
  • Save ptrxyz/b2de2ae1cb40faef2d8ad735a4a1d8ef to your computer and use it in GitHub Desktop.
Save ptrxyz/b2de2ae1cb40faef2d8ad735a4a1d8ef to your computer and use it in GitHub Desktop.
Docker Workshop App1
#!/usr/bin/env python3
from flask import Flask
import os
app = Flask(__name__)
class EnvNotSetError(Exception):
def __repr__(self):
return "EnvNotSetException was raised."
pass
def getMessage():
configfile = os.getenv("APPCONFIG")
if not configfile:
raise EnvNotSetError()
with open(configfile, "r") as f:
lines=[x[8:].strip() for x in f.readlines() if x.startswith("Message=")]
return lines[0]
@app.route("/")
def index():
message = getMessage()
try:
message = getMessage()
except FileNotFoundError:
message = "CONFIGURATION FILE NOT FOUND! THIS IS NOT OK!"
except IndexError:
message = "CONFIGURATION FILE FOUND BUT NO MESSAGE! THIS IS NOT OK!"
except EnvNotSetError:
message = "ENVIRONMENT VARIABLE NOT SET! THIS IS NOT OK!"
except Exception as ex:
message = f"BIG ERROR: {ex}"
return f"Hello! I have a Message for you. It reads:\n\n{message}"
app.run(host="0.0.0.0", port=3000, debug=True)
Message=Hello from the other side!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment