-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·89 lines (69 loc) · 2.19 KB
/
Copy pathserver.py
File metadata and controls
executable file
·89 lines (69 loc) · 2.19 KB
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
89
#!/usr/bin/env python3
from flask import Flask
from flask import request
from flask import Response
import json
import os
app = Flask(__name__)
class FileStore:
"""Manages the backend store for event messages."""
def __init__(self, fname):
self._file = fname
def store_event(self, event):
"""Store the received event in the store"""
with open(self._file, 'a') as f:
f.write("{}\n".format(event))
def get_last_event(self):
"""Return the last stored event"""
with open(self._file) as f:
l = f.readlines()
if len(l):
return l[-1]
def handle_errors(func):
"""A decorator which handles failures in the HTTP controllers.
This decorator ensures that a JSON response with status code 500
is sent back in case of any error."""
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
return Response(
"'error':'{}'".format(e),
status=500,
mimetype="application/json"
)
return inner
@handle_errors
@app.route("/event", methods=['POST'])
def store_event():
"""
POST /event
Stores the received payload as is
"""
e = request.data.decode('utf-8')
fs.store_event(e)
return Response(status=200)
@handle_errors
@app.route("/lastEvent")
def last_event():
"""
GET /lastEvent
{
"ts": "<timestamp>",
"msg": "<event message>"
}
Stores the entire payload as a event as-is. Does not validate inputs.
Timestamp is an integer in string format: Milliseconds since unix epoch
"""
last_eve = fs.get_last_event()
return Response(last_eve, status=200, mimetype='application/json')
if __name__ == "__main__":
# store events inside 'events/events.log' from the current working directory
# can be made configurable in the future
cur_path = os.getcwd()
events_store_path = os.path.join(cur_path, "events")
if not os.path.exists(events_store_path):
os.makedirs(events_store_path)
events_store_file = os.path.join(events_store_path, "events.log")
fs = FileStore(events_store_file)
app.run()