-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
executable file
·71 lines (54 loc) · 1.59 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
#!/usr/bin/env python3
from flask import Flask, send_from_directory, request
import flask_restful as restful
from flask_cors import CORS
import time
app = Flask(__name__)
CORS(app)
api = restful.Api(app)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
history = [1] # TODO: use an ordered dict instead
history_patch = {1: {
'cursor': {},
'dom': [
{
'type': 'add', 'append': 1, 'id': 1, 'node':
{
'nodeType': 1, 'oid': 1873262997,
'tagName': 'H1',
'children': [{
'nodeType': 3, 'oid': 1550618946,
'textValue': 'A Collaborative Title'
}],
'attributes': {}
}
}
],
'id': 1
}}
@app.route('/')
def index():
return open('src/index.html').read()
@app.route('/<path:path>')
def send_js(path):
return send_from_directory('src', path)
@app.route('/history-push', methods=['POST'])
def history_push():
data = request.get_json()
print(data)
history.append(data['id'])
history_patch[data['id']] = data
return {'status': 200}
class history_get(restful.Resource):
def get(self, oid=0):
index = 0
if oid:
index = history.index(oid) + 1
while index == len(history):
time.sleep(0.1)
result = [history_patch[x] for x in history[index:]]
print('Get After', oid, ':', [x for x in history[index:]])
return result
api.add_resource(history_get, '/history-get/<int:oid>')
if __name__ == '__main__':
app.run(port=8000, debug=True)