-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
514 lines (485 loc) · 21 KB
/
server.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from flask import Flask, request, jsonify
import sqlite3
import threading
import uuid
import dotenv
import os
import hashlib
import json
import time
app = Flask(__name__)
# Load the environment variables
dotenv.load_dotenv()
TREE_FILE = os.getenv('TREE_FILE')
TREE_JSON = os.getenv('TREE_JSON')
TREE_ID = os.getenv('TREE_ID')
SERVER_PASSWORD_HASH = hashlib.sha256(os.getenv('SERVER_PASSWORD').encode()).hexdigest()
SERVER_PORT = os.getenv('SERVER_PORT')
# If TREE_JSON is specified, delete the existing database
if TREE_JSON:
if os.path.exists(TREE_JSON):
# Delete the existing database
if os.path.exists(TREE_FILE):
os.remove(TREE_FILE)
# Create the nodes table
conn = sqlite3.connect(TREE_FILE)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS nodes
(id TEXT PRIMARY KEY,
parent_ids TEXT,
children_ids TEXT,
text TEXT,
author TEXT,
timestamp TEXT)''')
conn.commit()
conn.close()
# Create the history table (just node ids, timestamps, and operations)
conn = sqlite3.connect(TREE_FILE)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS history
(id TEXT PRIMARY KEY,
timestamp TEXT,
operation TEXT,
author TEXT)''')
# If TREE_JSON exists, load it into the database
if TREE_JSON:
if os.path.exists(TREE_JSON):
with open(TREE_JSON, encoding='utf-8') as f:
tree_json = json.load(f)
# print(tree_json)
# get current timestamp
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
conn = sqlite3.connect(TREE_FILE)
c = conn.cursor()
for node in tree_json['nodes']:
node_data = tree_json['nodes'][node]
# get parent id(s)
if 'parentIds' in node_data:
parent_ids = ','.join(node_data['parentIds'])
else:
parent_ids = node_data['parentId']
# get children id(s)
if 'childrenIds' in node_data:
children_ids = ','.join(node_data['childrenIds'])
else:
# we have to find the children ids from the tree_json
children_ids = []
for child in tree_json['nodes']:
if 'parentIds' in tree_json['nodes'][child]:
if node in tree_json['nodes'][child]['parentIds']:
children_ids.append(child)
children_ids = ','.join(children_ids)
# insert the node into the database
c.execute("INSERT INTO nodes (id, parent_ids, children_ids, text, author, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
(node, parent_ids, children_ids, node_data['text'], "Morpheus", timestamp))
conn.commit()
conn.close()
# Define a function to check if a user is authorized to make changes to the database
def is_authorized(key):
# Check if the key is valid
if hashlib.sha256(key.encode()).hexdigest() == SERVER_PASSWORD_HASH:
return True
else:
return False
# Define a function to create a new connection and cursor object
def get_db():
if not hasattr(threading.current_thread(), 'sqlite_db'):
threading.current_thread().sqlite_db = sqlite3.connect('tree.db')
return threading.current_thread().sqlite_db, threading.current_thread().sqlite_db.cursor()
# Define a function to close the connection when the request is finished
@app.teardown_appcontext
def close_db(error):
if hasattr(threading.current_thread(), 'sqlite_db'):
threading.current_thread().sqlite_db.close()
# Define a route for saving a new node to the database
@app.route('/nodes', methods=['POST'])
def save_node():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
data = request.get_json()
print(data)
# get parent id(s)
if 'parentIds' in data:
parent_ids = ','.join(data['parentIds'])
else:
parent_ids = data['parentId']
# get children id(s)
if 'childrenIds' in data:
children_ids = ','.join(data['childrenIds'])
else:
children_ids = ''
text = data['text']
author = data['author']
timestamp = data['timestamp']
if 'id' in data:
node_id = data['id']
else:
# generate a new id for the node
node_id = uuid.uuid4().hex
c.execute("INSERT INTO nodes (id, parent_ids, children_ids, text, author, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
(node_id, parent_ids, children_ids, text, author, timestamp))
# Add the operation to the history table
c.execute("INSERT INTO history (timestamp, id, operation, author) VALUES (?, ?, ?, ?)",
(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), node_id, 'create', author))
db.commit()
return jsonify({'success': True})
# Define a route for saving a set of new nodes to the database
@app.route('/nodes/batch', methods=['POST'])
def save_nodes():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
data = request.get_json()
for node in data:
# get parent id(s)
if 'parentIds' in node:
parent_ids = ','.join(node['parentIds'])
else:
parent_ids = node['parentId']
# get children id(s)
if 'childrenIds' in node:
children_ids = ','.join(node['childrenIds'])
else:
children_ids = ''
text = node['text']
author = node['author']
timestamp = node['timestamp']
if 'id' in node:
node_id = node['id']
else:
# generate a new id for the node
node_id = uuid.uuid4().hex
c.execute("INSERT INTO nodes (id, parent_ids, children_ids, text, author, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
(node_id, parent_ids, children_ids, text, author, timestamp))
# Add the operation to the history table
c.execute("INSERT INTO history (id, timestamp, operation, author) VALUES (?, ?, ?, ?)",
(node_id, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), 'create', author))
db.commit()
return jsonify({'success': True})
# Define a route for updating an existing node in the database
@app.route('/nodes/<node_id>', methods=['PUT'])
def update_node(node_id):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
data = request.get_json()
text = data['text']
author = data['author']
timestamp = data['timestamp']
c.execute("UPDATE nodes SET text = ?, author = ?, timestamp = ? WHERE id = ?",
(text, author, timestamp, node_id))
# Add the operation to the history table
c.execute("INSERT INTO history (timestamp, id, operation, author) VALUES (?, ?, ?, ?)",
(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), node_id, 'update', author))
db.commit()
return jsonify({'success': True})
# Define a route for updating a set of existing nodes in the database
@app.route('/nodes/batch', methods=['PUT'])
def update_nodes():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
data = request.get_json()
for node in data:
node_id = node['id']
text = node['text']
author = node['author']
timestamp = node['timestamp']
c.execute("UPDATE nodes SET text = ?, author = ?, timestamp = ? WHERE id = ?",
(text, author, timestamp, node_id))
# Add the operation to the history table
c.execute("INSERT INTO history (timestamp, id, operation, author) VALUES (?, ?, ?, ?)",
(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), node_id, 'update', author))
db.commit()
return jsonify({'success': True})
# Define a route for deleting a node from the database
@app.route('/nodes/<node_id>', methods=['DELETE'])
def delete_node(node_id):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("DELETE FROM nodes WHERE id = ?", (node_id,))
# Add the operation to the history table
author = request.args.get('author')
c.execute("INSERT INTO history (timestamp, id, operation, author) VALUES (?, ?, ?, ?)",
(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), node_id, 'delete', author))
db.commit()
return jsonify({'success': True})
# Define a route for deleting a set of nodes from the database
@app.route('/nodes/batch', methods=['DELETE'])
def delete_nodes():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
data = request.get_json()
for node in data:
node_id = node['id']
c.execute("DELETE FROM nodes WHERE id = ?", (node_id,))
# Add the operation to the history table
author = request.args.get('author')
c.execute("INSERT INTO history (timestamp, id, operation, author) VALUES (?, ?, ?, ?)",
(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), node_id, 'delete', author))
db.commit()
return jsonify({'success': True})
# Define a route for checking if a node exists in the database
@app.route('/nodes/exists/<node_id>', methods=['GET'])
def node_exists(node_id):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes WHERE id = ?", (node_id,))
node = c.fetchone()
if node:
return jsonify({'success': True, 'exists': True})
else:
return jsonify({'success': True, 'exists': False})
# Define a route for checking if a list of nodes exists in the database
@app.route('/nodes/exists', methods=['POST'])
def nodes_exist():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
data = request.get_json()
node_ids = data['nodeIds']
exists = {}
for node_id in node_ids:
c.execute("SELECT * FROM nodes WHERE id = ?", (node_id,))
node = c.fetchone()
if node:
exists[node_id] = True
else:
exists[node_id] = False
return jsonify({'success': True, 'exists': exists})
# Define a route for getting all nodes from the database after a given timestamp
@app.route('/nodes/get/<timestamp>', methods=['GET'])
def get_nodes(timestamp):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes WHERE timestamp > ?", (timestamp.replace("%"," "),))
nodes = c.fetchall()
# jsonify the nodes
nodes = [{
'id': node[0],
'parent_ids': node[1].split(',') if node[1] else None,
'children_ids': node[2].split(',') if node[2] else None,
'text': node[3],
'author': node[4],
'timestamp': node[5]
} for node in nodes]
return jsonify({'success': True, 'nodes': nodes})
# Define a route for getting all node ids from the database
@app.route('/nodes/ids', methods=['GET'])
def get_all_node_ids():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT id FROM nodes")
nodes = c.fetchall()
# jsonify the nodes
nodes = [node[0] for node in nodes]
return jsonify({'success': True, 'nodes': nodes})
# Define a route for getting all nodes from the database
@app.route('/nodes', methods=['GET'])
def get_all_nodes():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes")
nodes = c.fetchall()
# jsonify the nodes
nodes = {node[0]:{
'parent_ids': node[1].split(',') if node[1] else None,
'children_ids': node[2].split(',') if node[2] else None,
'text': node[3],
'author': node[4],
'timestamp': node[5]
} for node in nodes}
return jsonify({'success': True, 'nodes': nodes})
# Define a route for getting the number of nodes in the database
@app.route('/nodes/count', methods=['GET'])
def get_node_count():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT COUNT(*) FROM nodes")
count = c.fetchone()[0]
return jsonify({'success': True, 'count': count})
# Define a route for getting a single node from the database
@app.route('/nodes/<node_id>', methods=['GET'])
def get_node(node_id):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes WHERE id = ?", (node_id,))
node = c.fetchone()
# jsonify the node
node = {
'id': node[0],
'parent_ids': node[1].split(',') if node[1] else None,
'children_ids': node[2].split(',') if node[2] else None,
'text': node[3],
'author': node[4],
'timestamp': node[5]
}
return jsonify({'success': True, 'node': node})
# Define a route for getting the root node from the database
@app.route('/nodes/root', methods=['GET'])
def get_root_node():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes WHERE parent_ids IS NULL")
node = c.fetchone()
# jsonify the node
node = {
'id': node[0],
'parent_ids': node[1].split(',') if node[1] else None,
'children_ids': node[2].split(',') if node[2] else None,
'text': node[3],
'author': node[4],
'timestamp': node[5]
}
return jsonify({'success': True, 'node': node})
# Define a route for getting the children of a node from the database
@app.route('/nodes/<node_id>/children', methods=['GET'])
def get_children(node_id):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes WHERE parent_ids LIKE ?", ('%'+node_id+'%',))
nodes = c.fetchall()
# jsonify the nodes
nodes = [{
'id': node[0],
'parent_ids': node[1].split(',') if node[1] else None,
'children_ids': node[2].split(',') if node[2] else None,
'text': node[3],
'author': node[4],
'timestamp': node[5]
} for node in nodes]
return jsonify({'success': True, 'nodes': nodes})
# Define a route for getting the parents of a node from the database
@app.route('/nodes/<node_id>/parents', methods=['GET'])
def get_parents(node_id):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM nodes WHERE children_ids LIKE ?", ('%'+node_id+'%',))
nodes = c.fetchall()
# jsonify the nodes
nodes = [{
'id': node[0],
'parent_ids': node[1].split(',') if node[1] else None,
'children_ids': node[2].split(',') if node[2] else None,
'text': node[3],
'author': node[4],
'timestamp': node[5]
} for node in nodes]
return jsonify({'success': True, 'nodes': nodes})
# Define a route for getting the history from the database
@app.route('/history', methods=['GET'])
def get_history():
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM history")
history = c.fetchall()
# jsonify the history
history = [{
'node_id': h[0],
'timestamp': h[1],
'operation': h[2],
'author': h[3]
} for h in history]
return jsonify({'success': True, 'history': history})
# Define a route for getting the history from the database after a certain timestamp
@app.route('/history/<timestamp>', methods=['GET'])
def get_history_after(timestamp):
# Check if the user is authorized to make changes to the database
if not is_authorized(request.headers.get('Authorization')):
return jsonify({'success': False, 'error': 'Unauthorized'})
# Check if the tree id is correct
if request.headers.get('Tree-Id') != TREE_ID:
return jsonify({'success': False, 'error': 'Invalid Tree-Id'})
db, c = get_db()
c.execute("SELECT * FROM history WHERE timestamp > ?", (timestamp,))
history = c.fetchall()
# jsonify the history
history = [{
'node_id': h[0],
'timestamp': h[1],
'operation': h[2],
'author': h[3]
} for h in history]
return jsonify({'success': True, 'history': history})
app.run(host="0.0.0.0", port=SERVER_PORT, debug=True)