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

65 lines
1.4 KiB
Python
Raw Normal View History

2019-12-23 12:08:32 +01:00
#!/usr/bin/env python
# coding=utf-8
from flask import Flask
from flask import make_response
import json
from flask_wtf.csrf import CSRFProtect
2019-12-23 12:08:32 +01:00
from werkzeug.exceptions import NotFound
# OpenRefactory Warning: The 'Flask' method creates a Flask app
# without Cross-Site Request Forgery (CSRF) protection.
2019-12-23 12:08:32 +01:00
app = Flask(__name__)
CSRFProtect(app)
2019-12-23 12:08:32 +01:00
with open("./users.json", "r") as f:
users = json.load(f)
@app.route("/", methods=['GET'])
def index():
return pretty_json({
"resources": {
"users": "/users",
"user": "/users/<username>",
},
"current_uri": "/"
})
@app.route("/users", methods=['GET'])
def all_users():
return pretty_json(users)
@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):
response = make_response(json.dumps(arg, sort_keys=True, indent=4))
response.headers['Content-type'] = "application/json"
return response
def create_test_app():
# OpenRefactory Warning: The 'Flask' method creates a Flask app
# without Cross-Site Request Forgery (CSRF) protection.
2019-12-23 12:08:32 +01:00
app = Flask(__name__)
CSRFProtect(app)
2019-12-23 12:08:32 +01:00
return app
if __name__ == "__main__":
app.run(port=5000)