55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from flask import Flask, abort, request, make_response
|
|
import json
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
|
|
values = {}
|
|
|
|
config = {}
|
|
|
|
if "IOT_DATA_CONFIG" in os.environ:
|
|
config = json.loads(open(os.environ["IOT_DATA_CONFIG"]).read())
|
|
|
|
# just make the key lookup faster
|
|
keys = {}
|
|
for name, c in config.items():
|
|
if "key" in c:
|
|
if c["key"] in keys:
|
|
raise Exception("Keys need to be unique")
|
|
else:
|
|
keys[c["key"]] = name
|
|
|
|
@app.route("/ingress/<string:key>/", methods=["POST"])
|
|
def ingress(key):
|
|
if key in keys:
|
|
content_type = "text/plain"
|
|
if "content-type" in request.headers:
|
|
content_type = request.headers["content-type"]
|
|
|
|
values[keys[key]] = {
|
|
"payload": request.data,
|
|
"content-type": content_type,
|
|
}
|
|
else:
|
|
abort(401)
|
|
|
|
resp = make_response("", 201)
|
|
return resp
|
|
|
|
@app.route("/data/<string:name>/")
|
|
def data(name):
|
|
if not name in config:
|
|
abort(404)
|
|
if not name in values:
|
|
resp = make_response("", 204)
|
|
return resp
|
|
resp = make_response(values[name]["payload"], 200)
|
|
resp.headers['content-type'] = values[name]["content-type"]
|
|
return resp
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|