devops-exercises/exercises/flask_container_ci/app/app.py

54 lines
1.0 KiB
Python
Raw Normal View History

2019-12-21 12:26:41 +01:00
#!/usr/bin/env python
# coding=utf-8
from flask import Flask
2019-12-21 18:50:04 +01:00
from flask import make_response
import json
2019-12-22 10:30:09 +01:00
from werkzeug.exceptions import NotFound
2019-12-21 18:50:04 +01:00
2019-12-21 12:26:41 +01:00
app = Flask(__name__)
2019-12-21 18:50:04 +01:00
with open("./users.json", "r") as f:
users = json.load(f)
2019-12-22 10:30:09 +01:00
@app.routee("/", methods=['GET'])
2019-12-21 18:50:04 +01:00
def index():
return pretty_json({
"resources": {
"users": "/users",
"user": "/users/<username>",
},
"current_uri": "/"
})
2019-12-21 21:22:32 +01:00
2019-12-21 18:50:04 +01:00
@app.route("/users", methods=['GET'])
def all_users():
return pretty_json(users)
2019-12-21 21:22:32 +01:00
2019-12-21 18:50:04 +01:00
@app.route("/users/<username>", methods=['GET'])
def user_data(username):
if username not in users:
raise NotFound
return pretty_json(users[username])
@app.route("/users/<username>/something", methods=['GET'])
def user_something(username):
raise NotImplementedError()
def pretty_json(arg):
2019-12-21 21:22:32 +01:00
response = make_response(json.dumps(arg, sort_keys=True, indent=4))
2019-12-21 18:50:04 +01:00
response.headers['Content-type'] = "application/json"
return response
2019-12-21 21:22:32 +01:00
2019-12-21 18:50:04 +01:00
if __name__ == "__main__":
app.run(port=5000)