-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservertest.py
More file actions
99 lines (83 loc) · 2.88 KB
/
servertest.py
File metadata and controls
99 lines (83 loc) · 2.88 KB
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
from flask import Flask, render_template , jsonify, request, abort
from flask_socketio import SocketIO, emit
import json
import os
import re
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(BASE_DIR, 'saved_projects')
os.makedirs(PROJECT_DIR, exist_ok=True)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/insert')
def insert_page():
return render_template('test_insert.html')
@app.route('/')
def index():
return render_template('workspace.html')
# ---------- Save / Load Project API ----------
SAFE_NAME_PATTERN = re.compile(r'^[A-Za-z0-9_-]{1,64}$')
def safe_project_filename(name: str) -> str:
if not SAFE_NAME_PATTERN.match(name):
abort(400, description='Invalid project name')
return os.path.join(PROJECT_DIR, name + '.json')
@app.route('/api/save', methods=['POST'])
def api_save():
data = request.get_json(silent=True) or {}
name = data.get('name')
html = data.get('html')
if not name or html is None:
return jsonify({'error': 'name and html required'}), 400
path = safe_project_filename(name)
payload = {
'name': name,
'html': html
}
with open(path, 'w', encoding='utf-8') as f:
json.dump(payload, f, ensure_ascii=False, indent=2)
return jsonify({'status': 'ok', 'name': name})
@app.route('/api/list', methods=['GET'])
def api_list():
files = []
for fname in os.listdir(PROJECT_DIR):
if fname.endswith('.json'):
files.append(fname[:-5])
return jsonify({'projects': sorted(files)})
@app.route('/api/load', methods=['GET'])
def api_load():
name = request.args.get('name')
if not name:
return jsonify({'error': 'name required'}), 400
path = safe_project_filename(name)
if not os.path.isfile(path):
return jsonify({'error': 'not found'}), 404
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
return jsonify({'name': data.get('name'), 'html': data.get('html')})
@socketio.on('update content')
def handle_content(data):
global html_content
html_content = data
emit('update content', data, broadcast=True)
@socketio.on('update position')
def handle_position(data):
global div_position
div_position = data
emit('update position', data, broadcast=True)
def read_counters():
with open('data.json', 'r') as file:
data = json.load(file)
return data.get('counters', {})
def update_counters(counters):
with open('data.json', 'w') as file:
json.dump({'counters': counters}, file)
@app.route('/get-id', methods=['GET'])
def get_id():
value = request.args.get('value')
counters = read_counters()
counter = counters.get(value, 0) + 1
counters[value] = counter
update_counters(counters)
return jsonify({'id': f'{counter}'})
if __name__ == '__main__':
socketio.run(app, host="127.0.0.1", debug=True)