Created
April 11, 2022 01:28
-
-
Save ptrxyz/692c6d4e86b5d01a6f029a327501e630 to your computer and use it in GitHub Desktop.
Docker Workshop 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from flask import Flask | |
import os | |
import redis | |
import time | |
app = Flask(__name__) | |
cache = redis.Redis(host='redis', port=6379) | |
class EnvNotSetError(Exception): | |
def __repr__(self): | |
return "EnvNotSetException was raised." | |
pass | |
def getHitCount(): | |
retries = 5 | |
while True: | |
try: | |
return cache.incr('hits') | |
except redis.exceptions.ConnectionError as exc: | |
if retries == 0: | |
raise exc | |
retries -= 1 | |
time.sleep(0.5) | |
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(): | |
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}" | |
count = getHitCount() | |
return f"Hello! I have a Message for you. It reads:\n\n{message}\n\nI have seen you {count} times!" | |
app.run(host="0.0.0.0", port=3000, debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.9" | |
services: | |
web: | |
... | |
build from here, mount volumes, set environments, set ports, | |
... | |
redis: | |
image: "redis:alpine" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.7-alpine | |
RUN apk add --no-cache gcc musl-dev linux-headers | |
...add files, set env, install stuff, etc... | |
RUN pip install -r requirements.txt | |
...add files, set env, install stuff, etc... | |
CMD python app.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flask | |
redis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment