-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
60 lines (49 loc) · 1.47 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
import os
import boto3
import epsagon
import json
import logging
import watchtower
from flask import Flask, request, abort
epsagon.init(
token=os.environ.get('EPSAGON_KEY'),
app_name='order_app',
metadata_only=False
)
app = Flask(__name__)
epsagon.flask_wrapper(app)
sns = boto3.client(
'sns',
region_name='us-east-2',
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY')
)
logs_session = boto3.Session(
region_name='us-east-2',
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY')
)
logging.basicConfig(level=logging.INFO)
app = Flask("loggable")
handler = watchtower.CloudWatchLogHandler(boto3_session=logs_session)
app.logger.addHandler(handler)
logging.getLogger("werkzeug").addHandler(handler)
@app.route('/order', methods = ['POST'])
def order():
try:
order_payload = json.loads(request.data)
except Exception as e:
app.logger.error(f'Unable to parse payload {request.data}')
app.logger.error(e)
abort(400)
try:
sns.publish(
TopicArn=os.environ.get('APP_TOPIC'),
Message=json.dumps(order_payload),
)
return 'Your Order is being processed', 200
except Exception as e:
app.logger.error(e)
abort(500, 'Unable to find the specified topic')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')