-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
88 lines (66 loc) · 1.78 KB
/
app.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Example greeting micro-service.
This module implements a greeting service to be used for demos.
Example:
For a "Hello World", this application would return "Hello".
Other options would include "Hey" or "Bonjour".
"""
import json
from flask import Flask
from flask_cors import CORS
HTTP_OK = 200
APP_VERSION = 'dev'
app = Flask(__name__)
cors = CORS(app)
@app.route('/status/alive', methods=['GET'])
def alive():
"""Status check function to verify the server can start.
Returns:
JSON-formated response.
"""
response = {
'status': 'Greeter service is alive',
}
return app.response_class(
response=json.dumps(response),
status=HTTP_OK,
mimetype='application/json',
)
@app.route('/status/healthy', methods=['GET'])
def healthy():
"""Status check function to verify the server can serve requests.
Returns:
JSON-formated response.
"""
response = {
'status': 'Greeter service is healthy',
}
return app.response_class(
response=json.dumps(response),
status=HTTP_OK,
mimetype='application/json',
)
@app.route('/', methods=['GET'])
def index():
"""Return a greeting.
Returns:
JSON-formated response.
"""
greeting = {
'greeting': 'hello',
}
return app.response_class(
response=json.dumps(greeting),
status=HTTP_OK,
mimetype='application/json',
)
@app.after_request
def after_request_func(response):
"""Add helpful headers to the response.
Args:
response: the Flask-provided response object.
Returns:
Proper response, with added headers.
"""
response.headers['X-Reply-Service'] = 'greeter-service'
response.headers['X-Version'] = APP_VERSION
return response