-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
80 lines (64 loc) · 2.01 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
import os
from config_helper import AppConfigHelper
from flask import Flask
import boto3
from botocore.exceptions import ClientError
app = Flask(__name__)
# Attempt to refresh config every 1 hour
CONFIG_REFRESH_TIME = 3600
appconfig = AppConfigHelper(
os.environ['ConfigApp'],
os.environ['ConfigEnv'],
os.environ['ConfigProfile'],
CONFIG_REFRESH_TIME,
os.environ['ConfigClient']
)
@app.route('/')
def health():
"""
Health check endpoint for the load balancer to poll
"""
return "All good !"
@app.route('/hello')
def hello_world():
"""
Display hello message using the information from the Dynamo table
"""
ddb_client = boto3.client(
'dynamodb', region_name=os.environ['AWS_DEFAULT_REGION'])
TABLE_NAME = get_table_name()
try:
response = ddb_client.get_item(
TableName=TABLE_NAME,
Key={'Application': {'S': 'TwelveFactorApp'}})
return f"<html><body style=\"background-color"\
f":{response['Item']['BgColor']['S']};color:white;text-align:center\">"\
f"<p><strong><h1>Hello from {response['Item']['Name']['S']}!</h1></strong></p>"\
f"<p><h2>Developed with {response['Item']['Language']['S']},"\
f" deployed with {response['Item']['Platform']['S']}"\
"</h2></p></body></html>"
except ClientError as e:
return response['Error']['Message']
def get_table_name():
"""
Get table name from App Config
"""
appconfig.update_config()
return appconfig.config["TableName"]
@app.route('/table-name')
def table_name():
"""
Return table name using API
"""
return get_table_name()
@app.route('/refresh-config')
def refresh():
"""
Force refresh config using the API endpoint
"""
result = "Config Refreshed" if appconfig.update_config(
force=True) else "Nothing to refresh"
return result
# Bind the Flask application to port 80 of the runtime environment
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)