forked from wizelineacademy/sre-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
45 lines (34 loc) · 918 Bytes
/
api.py
File metadata and controls
45 lines (34 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from flask import Flask
from flask import jsonify
from flask import request
from methods import Token, Restricted
app = Flask(__name__)
login = Token()
protected = Restricted()
# Just a health check
@app.route("/")
def url_root():
return "OK"
# Just a health check
@app.route("/_health")
def url_health():
return "OK"
# e.g. http://127.0.0.1:8000/login
@app.route("/login", methods=['POST'])
def url_login():
username = request.form['username']
password = request.form['password']
res = {
"data": login.generate_token(username, password)
}
return jsonify(res)
# # e.g. http://127.0.0.1:8000/protected
@app.route("/protected")
def url_protected():
auth_token = request.headers.get('Authorization')
res = {
"data": protected.access_data(auth_token)
}
return jsonify(res)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)