forked from kidicarus2/CableMonkeyPacket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
151 lines (124 loc) · 4.04 KB
/
Copy pathserver.js
File metadata and controls
151 lines (124 loc) · 4.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
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
const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;
const DATA_FILE = 'annotations.json';
// Initialize data file if it doesn't exist
if (!fs.existsSync(DATA_FILE)) {
fs.writeFileSync(DATA_FILE, JSON.stringify([]));
}
// Helper functions to read/write data
function readAnnotations() {
try {
const data = fs.readFileSync(DATA_FILE, 'utf8');
return JSON.parse(data);
} catch (err) {
return [];
}
}
function writeAnnotations(data) {
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
}
app.use(cors());
app.use(express.json());
// Serve static files from public directory
app.use(express.static(path.join(__dirname, 'public')));
// API: Get all annotations for a document
app.get('/api/annotations/:docSlug', (req, res) => {
const { docSlug } = req.params;
try {
const allAnnotations = readAnnotations();
const annotations = allAnnotations.filter(a => a.doc_slug === docSlug);
res.json(annotations);
} catch (err) {
console.error('Error fetching annotations:', err);
res.status(500).json({ error: 'Failed to fetch annotations' });
}
});
// API: Create new annotation
app.post('/api/annotations', (req, res) => {
const { docSlug, page, x, y, text } = req.body;
if (!docSlug || !page || x === undefined || y === undefined || !text) {
return res.status(400).json({ error: 'Missing required fields' });
}
try {
const allAnnotations = readAnnotations();
const created = new Date().toISOString();
const id = Date.now(); // Simple ID generation
const newAnnotation = {
id,
doc_slug: docSlug,
page,
x,
y,
text,
created,
replies: []
};
allAnnotations.push(newAnnotation);
writeAnnotations(allAnnotations);
res.json(newAnnotation);
} catch (err) {
console.error('Error creating annotation:', err);
res.status(500).json({ error: 'Failed to create annotation' });
}
});
// API: Add reply to annotation
app.post('/api/annotations/:id/reply', (req, res) => {
const { id } = req.params;
const { text } = req.body;
if (!text) {
return res.status(400).json({ error: 'Reply text required' });
}
try {
const allAnnotations = readAnnotations();
const idx = allAnnotations.findIndex(a => a.id == id);
if (idx === -1) {
return res.status(404).json({ error: 'Annotation not found' });
}
const newReply = {
text,
created: new Date().toISOString()
};
if (!allAnnotations[idx].replies) {
allAnnotations[idx].replies = [];
}
allAnnotations[idx].replies.push(newReply);
writeAnnotations(allAnnotations);
res.json(newReply);
} catch (err) {
console.error('Error adding reply:', err);
res.status(500).json({ error: 'Failed to add reply' });
}
});
// API: Delete annotation
app.delete('/api/annotations/:id', (req, res) => {
const { id } = req.params;
try {
let allAnnotations = readAnnotations();
const initialLength = allAnnotations.length;
allAnnotations = allAnnotations.filter(a => a.id != id);
if (allAnnotations.length === initialLength) {
return res.status(404).json({ error: 'Annotation not found' });
}
writeAnnotations(allAnnotations);
res.json({ success: true });
} catch (err) {
console.error('Error deleting annotation:', err);
res.status(500).json({ error: 'Failed to delete annotation' });
}
});
// Serve Hugo site for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
console.log('API endpoints:');
console.log(' GET /api/annotations/:docSlug - Get annotations');
console.log(' POST /api/annotations - Create annotation');
console.log(' POST /api/annotations/:id/reply - Add reply');
console.log(' DELETE /api/annotations/:id - Delete annotation');
});