38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import json
|
|
|
|
def main(args):
|
|
route = args.get("route")
|
|
|
|
# Verify file exists, if not create it with default content
|
|
try:
|
|
with open("/app/state.json", "r") as f:
|
|
pass
|
|
except FileNotFoundError:
|
|
with open("/app/state.json", "w") as f:
|
|
json.dump({"text": ""}, f)
|
|
f.flush()
|
|
|
|
body = args.get("body")
|
|
body = json.loads(body) if body else {}
|
|
|
|
if route.startswith("push"):
|
|
if route == "push_text":
|
|
with open("/app/state.json", "r") as f:
|
|
current = json.load(f)
|
|
current["text"] = body.get("text", "")
|
|
with open("/app/state.json", "w") as f:
|
|
json.dump(current, f)
|
|
return {"status": "success"}
|
|
else:
|
|
return {"status": "error", "message": "Unknown push route"}
|
|
elif route.startswith("pull"):
|
|
if route == "pull_text":
|
|
with open("/app/state.json", "r") as f:
|
|
current = json.load(f)
|
|
return {"status": "success", "text": current.get("text", "")}
|
|
elif route == "pull_full":
|
|
with open("/app/state.json", "r") as f:
|
|
current = json.load(f)
|
|
return {"status": "success", "state": current}
|
|
else:
|
|
return {"status": "error", "message": "Unknown pull route"} |