-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(examples): more pythonic code-style and formatting
- Loading branch information
1 parent
33e2b98
commit 199d8cc
Showing
2 changed files
with
90 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
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) |
This file contains 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
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) |