54 lines
1.8 KiB
Python
Executable File
54 lines
1.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
from flask import Flask, make_response, request
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
app = Flask(__name__)
|
|
database_node_port = 5002
|
|
basic_path = "./files/db"
|
|
|
|
|
|
@app.route('/users/<string:username>/<string:field>', methods=["GET", "POST"])
|
|
def users(username, field):
|
|
path = basic_path + "/users/" + username
|
|
if request.method == 'GET':
|
|
if not os.path.exists(path):
|
|
return make_response({"status": "user_not_found"}, 404)
|
|
with open(path, "r") as f:
|
|
data = json.loads(f.read())
|
|
if field not in data:
|
|
return make_response({"status": "field_not_found"}, 404)
|
|
return make_response({"status": "ok", "data": data[field]}, 200)
|
|
elif request.method == "POST":
|
|
if not os.path.exists(path):
|
|
Path(path).touch()
|
|
with open(path, "r+") as f:
|
|
try:
|
|
data = json.loads(f.read())
|
|
except json.JSONDecodeError:
|
|
data = {}
|
|
data[field] = request.json["value"]
|
|
with open(path, "w") as f:
|
|
f.write(json.dumps(data))
|
|
return make_response({"status": "ok"}, 200)
|
|
|
|
|
|
@app.route('/images/<string:id>', methods=["GET", "POST"])
|
|
def images(id):
|
|
path = basic_path + "/images/" + id.zfill(2)
|
|
if request.method == 'GET':
|
|
with open(path, "r") as f:
|
|
data = json.loads(f.read())
|
|
return make_response({"status": "ok", "data": data}, 200)
|
|
elif request.method == "POST":
|
|
with open(path, "r+") as f:
|
|
data = json.loads(f.read())
|
|
data["comments"].append(request.json["value"])
|
|
with open(path, "w") as f:
|
|
f.write(json.dumps(data))
|
|
return make_response({"status": "ok"}, 200)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host="0.0.0.0", port=database_node_port)
|