-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
188 lines (163 loc) · 5.46 KB
/
Copy pathserver.js
File metadata and controls
188 lines (163 loc) · 5.46 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const express = require('express');
const parser = require('./parser');
const renderer = require('./renderer');
const serializer = require('./serializer');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
// Render and serve the page server-side
app.get('/', (req, res) => {
try {
const data = parser.load();
const html = renderer.render(data);
res.send(html);
} catch (err) {
res.status(500).send(`<pre>Error: ${err.message}</pre>`);
}
});
// Return raw parsed data as JSON (useful for debugging)
app.get('/data', (req, res) => {
try {
res.json(parser.load());
} catch (err) {
res.status(500).json({ error: 'Failed to load data file', detail: err.message });
}
});
// Mark a step as complete and persist to data.txt
// Body: { itemIndex, stepIndex } — root task step
// { itemIndex, taskIndex, stepIndex } — project task step
app.post('/complete-step', (req, res) => {
try {
const { itemIndex, taskIndex, stepIndex } = req.body;
const data = parser.load();
const task = taskIndex != null
? data.items[itemIndex].tasks[taskIndex]
: data.items[itemIndex];
if (!task || !task.steps[stepIndex]) {
return res.status(404).json({ error: 'Step not found' });
}
task.steps[stepIndex].done = true;
serializer.save(data);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Delete a task, project, or project task from data.txt
// Body: { itemIndex } — root task or whole project
// { itemIndex, taskIndex } — single task within a project
app.delete('/item', (req, res) => {
try {
const { itemIndex, taskIndex } = req.body;
const data = parser.load();
if (taskIndex != null) {
data.items[itemIndex].tasks.splice(taskIndex, 1);
} else {
data.items.splice(itemIndex, 1);
}
serializer.save(data);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Add a new task or project to data.txt
// Body (task): { type: 'task', text: string, steps: string[] }
// Body (project): { type: 'project', name: string, tasks: [{ text: string, steps: string[] }] }
app.post('/add-item', (req, res) => {
try {
const data = parser.load();
const { type } = req.body;
if (type === 'task') {
const { text, steps = [] } = req.body;
if (!text) return res.status(400).json({ error: 'text is required' });
data.items.push({
type: 'task',
text,
steps: steps.filter(Boolean).map(s => ({ type: 'step', text: s, done: false })),
});
} else if (type === 'project') {
const { name, tasks = [] } = req.body;
if (!name) return res.status(400).json({ error: 'name is required' });
data.items.push({
type: 'project',
name,
tasks: tasks.map(t => ({
type: 'task',
text: t.text,
steps: (t.steps || []).filter(Boolean).map(s => ({ type: 'step', text: s, done: false })),
})),
});
} else {
return res.status(400).json({ error: 'type must be "task" or "project"' });
}
serializer.save(data);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Persist the selected UI theme to data.txt
// Body: { theme: 'light' | 'dark' | 'dusk' | 'dawn' | 'forest' }
app.post('/set-theme', (req, res) => {
try {
const { theme } = req.body;
const valid = ['light', 'dark', 'dusk', 'dawn', 'forest'];
if (!valid.includes(theme)) return res.status(400).json({ error: 'Invalid theme' });
const data = parser.load();
data.user.theme = theme;
serializer.save(data);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Persist the user's name + description (role) to data.txt
// Body: { name: string, role: string }
app.post('/set-user', (req, res) => {
try {
const { name, role } = req.body || {};
if (!name || !String(name).trim()) return res.status(400).json({ error: 'name is required' });
const data = parser.load();
data.user.name = String(name).trim();
data.user.role = String(role || '').trim();
if (!data.user.theme) data.user.theme = 'light';
serializer.save(data);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Add a task to an existing project
// Body: { itemIndex, text, steps: string[] }
app.post('/add-project-task', (req, res) => {
try {
const { itemIndex, text, steps = [] } = req.body;
if (!text) return res.status(400).json({ error: 'text is required' });
const data = parser.load();
const project = data.items[itemIndex];
if (!project || project.type !== 'project') {
return res.status(404).json({ error: 'Project not found' });
}
project.tasks.push({
type: 'task',
text,
steps: steps.filter(Boolean).map(s => ({ type: 'step', text: s, done: false })),
});
serializer.save(data);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
const server = app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`\nPort ${PORT} is already in use. Kill the other process first:\n lsof -ti tcp:${PORT} | xargs kill\n`);
process.exit(1);
} else {
throw err;
}
});