/
echo.py
50 lines (41 loc) · 1.18 KB
/
echo.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
import json
import logging
import os
from falcon import API, Request, Response
class PingResource(object):
"""
Small resource for health checking.
"""
def on_get(self, req: Request, res: Response):
res.media = {"pong": True}
def sink(environment):
def and_the_kitchen_sink(req: Request, res: Response):
"""
Handle all request with their payload.
"""
res.media = {
"path": req.path,
"params": req.params,
"method": req.method,
"headers": req.headers,
"query": req.query_string,
"payload": json.loads(req.bounded_stream.read() or "{}"),
"tags": {
name: value
for name, value in environment.items()
if name.startswith("SERVICE_")
},
}
return and_the_kitchen_sink
def app(environment=None):
"""
Create our echo falcon app.
"""
if environment is None:
environment = os.environ
logging.basicConfig()
logging.getLogger("falcon").setLevel(logging.DEBUG)
api = API()
api.add_route("/ping", PingResource())
api.add_sink(sink(environment))
return api