Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: changed vote examples to use python 3 #3006

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/vote-helm/api-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Using official python runtime base image
FROM python:2.7-alpine
FROM python:3.10.5-alpine3.16

# Set the application directory
WORKDIR /app
Expand Down
86 changes: 45 additions & 41 deletions examples/vote-helm/api-image/app.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,61 @@
from flask import Flask, render_template, request, make_response, g
from flask_cors import CORS
from redis import Redis
import json
import os
import socket
import random
import json
import socket

from flask import Flask, request, g
from flask_cors import CORS
from redis import Redis

REDIS_HOST = 'redis-master'

option_a = os.getenv('OPTION_A', "Cats")
option_b = os.getenv('OPTION_B', "Dogs")
option_a = os.getenv('OPTION_A', 'Cats')
option_b = os.getenv('OPTION_B', 'Dogs')
hostname = socket.gethostname()

app = Flask(__name__)
CORS(app)


def get_redis():
if not hasattr(g, 'redis'):
g.redis = Redis(host=REDIS_HOST, db=0, socket_timeout=5)
return g.redis
if not hasattr(g, 'redis'):
g.redis = Redis(host=REDIS_HOST, db=0, socket_timeout=5)
return g.redis


@app.route("/api", methods=['GET'])
@app.route('/api', methods=['GET'])
def hello():
return app.response_class(
response='Hello, I am the api service',
status=200,
)


@app.route('/api/vote', methods=['POST', 'GET'])
def vote():
app.logger.info('Received request')

voter_id = hex(random.getrandbits(64))[2:-1]

if request.method == 'POST':
redis = get_redis()
vote = request.form['vote']
data = json.dumps({'voter_id': voter_id, 'vote': vote})

redis.rpush('votes', data)
app.logger.info('Registered vote')
return app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
else:
return app.response_class(
response="Hello, I am the api service",
status=200,
response=json.dumps({}),
status=404,
mimetype='application/json'
)

@app.route("/api/vote", methods=['POST','GET'])
def vote():
app.logger.info("received request")

voter_id = hex(random.getrandbits(64))[2:-1]
vote = None

if request.method == 'POST':
redis = get_redis()
vote = request.form['vote']
data = json.dumps({'voter_id': voter_id, 'vote': vote})

redis.rpush('votes', data)
print("Registered vote")
return app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
else:
return app.response_class(
response=json.dumps({}),
status=404,
mimetype='application/json'
)

if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True, threaded=True)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True, threaded=True)
2 changes: 1 addition & 1 deletion examples/vote/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Using official python runtime base image
FROM python:2.7-alpine
FROM python:3.10.5-alpine3.16

# Set the application directory
WORKDIR /app
Expand Down
86 changes: 45 additions & 41 deletions examples/vote/api/app.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
from flask import Flask, render_template, request, make_response, g
from flask_cors import CORS
from redis import Redis
import json
import os
import socket
import random
import json
import socket

option_a = os.getenv('OPTION_A', "Cats")
option_b = os.getenv('OPTION_B', "Dogs")
from flask import Flask, request, g
from flask_cors import CORS
from redis import Redis

option_a = os.getenv('OPTION_A', 'Cats')
option_b = os.getenv('OPTION_B', 'Dogs')
hostname = socket.gethostname()

app = Flask(__name__)
CORS(app)


def get_redis():
if not hasattr(g, 'redis'):
g.redis = Redis(host="redis", db=0, socket_timeout=5)
return g.redis
if not hasattr(g, 'redis'):
g.redis = Redis(host='redis', db=0, socket_timeout=5)
return g.redis


@app.route("/api", methods=['GET'])
@app.route('/api', methods=['GET'])
def hello():
return app.response_class(
response='Hello, I am the api service',
status=200,
)


@app.route('/api/vote', methods=['POST', 'GET'])
def vote():
app.logger.info('Received request')

voter_id = hex(random.getrandbits(64))[2:-1]

if request.method == 'POST':
redis = get_redis()
vote = request.form['vote']
data = json.dumps({'voter_id': voter_id, 'vote': vote})

redis.rpush('votes', data)
app.logger.info('Registered vote')
return app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
else:
return app.response_class(
response="Hello, I am the api service",
status=200,
response=json.dumps({}),
status=404,
mimetype='application/json'
)

@app.route("/api/vote", methods=['POST','GET'])
def vote():
app.logger.info("received request")

voter_id = hex(random.getrandbits(64))[2:-1]
vote = None

if request.method == 'POST':
redis = get_redis()
vote = request.form['vote']
data = json.dumps({'voter_id': voter_id, 'vote': vote})

redis.rpush('votes', data)
print("Registered vote")
return app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
else:
return app.response_class(
response=json.dumps({}),
status=404,
mimetype='application/json'
)

if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True, threaded=True)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True, threaded=True)