-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
61 lines (51 loc) · 2.21 KB
/
Copy pathserver.py
File metadata and controls
61 lines (51 loc) · 2.21 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
import http.server
import socketserver
import json
import os
from datetime import datetime
PORT = 8000
JSON_FILE = 'backend/reflections.json'
class JournalHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
if self.path == '/api/entries':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
new_entry = json.loads(post_data.decode('utf-8'))
# Add timestamp if not present
if 'timestamp' not in new_entry:
new_entry['timestamp'] = datetime.now().isoformat()
# Read existing entries
if os.path.exists(JSON_FILE):
with open(JSON_FILE, 'r') as f:
try:
entries = json.load(f)
except json.JSONDecodeError:
entries = []
else:
entries = []
# Append new entry
entries.append(new_entry)
# Save back to file
with open(JSON_FILE, 'w') as f:
json.dump(entries, f, indent=4)
# Send response
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
response = {'status': 'success', 'message': 'Entry saved successfully'}
self.wfile.write(json.dumps(response).encode('utf-8'))
except Exception as e:
self.send_response(500)
self.send_header('Content-type', 'application/json')
self.end_headers()
response = {'status': 'error', 'message': str(e)}
self.wfile.write(json.dumps(response).encode('utf-8'))
else:
self.send_error(404, "File not found")
print(f"Serving at http://localhost:{PORT}")
with socketserver.TCPServer(("", PORT), JournalHandler) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")