-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
40 lines (32 loc) · 1.04 KB
/
Copy pathapp.py
File metadata and controls
40 lines (32 loc) · 1.04 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
from flask import Flask, request, send_from_directory, jsonify
from flask_cors import CORS
import os
app = Flask(__name__)
CORS(app)
SAVE_FILE = "shared_notes.txt"
@app.route('/')
def home():
return send_from_directory('.', 'index.html')
@app.route('/save', methods=['POST'])
def save_message():
print("POST /save called")
data = request.get_json(force=True)
print("Data received:", data)
if not data or 'text' not in data:
return "Invalid data", 400
message = data['text'].strip()
if message:
with open(SAVE_FILE, "w", encoding="utf-8") as f: # overwrite instead of append
f.write(message + "\n")
return "Saved", 200
else:
return "Empty message", 400
@app.route('/load', methods=['GET'])
def load_message():
if not os.path.exists(SAVE_FILE):
return jsonify({"text": ""})
with open(SAVE_FILE, "r", encoding="utf-8") as f:
content = f.read().strip()
return jsonify({"text": content})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)