Pablo Solar VilariƱo
2020-05-28 8f1d0d6af42ddb0e22c5b05b977f31a8ecea136d
feat(ch07s09) - v2 of the gossip application and v2 of the currencies service (#21)

6 files copied
3 files added
9 files renamed
112 ■■■■■ changed files
exchange-application/currencies/v1/Dockerfile patch | view | raw | blame | history
exchange-application/currencies/v1/requirements.txt patch | view | raw | blame | history
exchange-application/currencies/v1/src/currencies.py patch | view | raw | blame | history
exchange-application/currencies/v2/Dockerfile patch | view | raw | blame | history
exchange-application/currencies/v2/requirements.txt patch | view | raw | blame | history
exchange-application/currencies/v2/src/currencies.py 27 ●●●●● patch | view | raw | blame | history
python-flask-gossip/v1/Dockerfile patch | view | raw | blame | history
python-flask-gossip/v1/README.md patch | view | raw | blame | history
python-flask-gossip/v1/kubefiles/gossip-application.yml patch | view | raw | blame | history
python-flask-gossip/v1/requirements.txt patch | view | raw | blame | history
python-flask-gossip/v1/src/data/finance.json patch | view | raw | blame | history
python-flask-gossip/v1/src/gossip.py patch | view | raw | blame | history
python-flask-gossip/v2/Dockerfile patch | view | raw | blame | history
python-flask-gossip/v2/README.md patch | view | raw | blame | history
python-flask-gossip/v2/kubefiles/gossip-application.yml 46 ●●●●● patch | view | raw | blame | history
python-flask-gossip/v2/requirements.txt patch | view | raw | blame | history
python-flask-gossip/v2/src/data/finance.json patch | view | raw | blame | history
python-flask-gossip/v2/src/gossip.py 39 ●●●●● patch | view | raw | blame | history
exchange-application/currencies/v1/Dockerfile
exchange-application/currencies/v1/requirements.txt
exchange-application/currencies/v1/src/currencies.py
exchange-application/currencies/v2/Dockerfile
copy from exchange-application/currencies/Dockerfile copy to exchange-application/currencies/v2/Dockerfile
exchange-application/currencies/v2/requirements.txt
copy from exchange-application/currencies/requirements.txt copy to exchange-application/currencies/v2/requirements.txt
exchange-application/currencies/v2/src/currencies.py
New file
@@ -0,0 +1,27 @@
import os
from flask import abort, Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
ERROR_DIVISOR = int(os.environ.get('ERROR_DIVISOR', 0))
ERROR_RESPONSE = int(os.environ.get('ERROR_RESPONSE', 500))
requestsCounter = 0
def failCheck():
    global requestsCounter
    if ERROR_DIVISOR and requestsCounter % ERROR_DIVISOR != 0 :
        abort(ERROR_RESPONSE)
def trackRequest():
    global requestsCounter
    requestsCounter += 1
@app.route('/')
def currenciesList():
    trackRequest()
    failCheck()
    return jsonify('EUR', 'USD')
python-flask-gossip/v1/Dockerfile
python-flask-gossip/v1/README.md
python-flask-gossip/v1/kubefiles/gossip-application.yml
python-flask-gossip/v1/requirements.txt
python-flask-gossip/v1/src/data/finance.json
python-flask-gossip/v1/src/gossip.py
python-flask-gossip/v2/Dockerfile
copy from python-flask-gossip/Dockerfile copy to python-flask-gossip/v2/Dockerfile
python-flask-gossip/v2/README.md
copy from python-flask-gossip/README.md copy to python-flask-gossip/v2/README.md
python-flask-gossip/v2/kubefiles/gossip-application.yml
New file
@@ -0,0 +1,46 @@
apiVersion: apps/v1
kind: Deployment
metadata:
  name: news
spec:
  selector:
    matchLabels:
      app: news
  replicas: 1
  template:
    metadata:
      labels:
        app: news
    spec:
      containers:
        - name: news
          image: quay.io/redhattraining/ossm-python-flask-gossip:2.0
          ports:
            - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: news
  name: news
spec:
  ports:
  - port: 5000
    protocol: TCP
    targetPort: 5000
  selector:
    app: news
---
kind: Route
apiVersion: route.openshift.io/v1
metadata:
  name: news
  labels:
    app: news
spec:
  to:
    kind: Service
    name: news
  port:
    targetPort: 5000
python-flask-gossip/v2/requirements.txt
copy from python-flask-gossip/requirements.txt copy to python-flask-gossip/v2/requirements.txt
python-flask-gossip/v2/src/data/finance.json
copy from python-flask-gossip/src/data/finance.json copy to python-flask-gossip/v2/src/data/finance.json
python-flask-gossip/v2/src/gossip.py
New file
@@ -0,0 +1,39 @@
import json
import os
import random
from flask import abort, Flask, jsonify
from flask_cors import CORS
from markupsafe import escape
app = Flask(__name__)
CORS(app)
NUM_OF_NEWS = 3
DATA_FOLDER = escape(os.environ.get('DATA_FOLDER', 'data'))
ERROR_RESPONSE = int(os.environ.get('ERROR_RESPONSE', 404))
def sortByTimestamp(element):
  return element['timestamp']
@app.errorhandler(404)
def topicNotFound(error):
    return "Unable to find the specific topic", 404
@app.route('/news/<string:topic>')
def getNewsForTopic(topic):
    try:
        # Loading the file which has the topic news
        with open('./%s/%s.json' % (DATA_FOLDER, escape(topic))) as topicFile:
            news = json.load(topicFile)
        # Picking only a few elements from the list of news
        randomSelection = random.sample(news['data'], NUM_OF_NEWS)
        # Sorting the resulting list by the timestamp
        randomSelection.sort(key=sortByTimestamp)
        return jsonify(randomSelection)
    # If we can not find a file for the topic, we throw an error
    except IOError:
        abort(ERROR_RESPONSE)