-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
303 lines (278 loc) · 11.5 KB
/
server.ts
File metadata and controls
303 lines (278 loc) · 11.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { readFile, writeFile } from "node:fs/promises";
const LINKS_FILE = "./links.json";
const STYLE_FILE = "./style.css";
const JS_FILE = "./main.js";
interface Link {
name: string;
url: string;
}
interface Category {
name: string;
links: Link[];
}
interface LinksData {
categories: Category[];
}
async function readLinks(): Promise<LinksData> {
try {
const data = await readFile(LINKS_FILE, { encoding: "utf-8" });
return JSON.parse(data);
} catch (error) {
console.error("Error reading links file:", error);
return { categories: [] };
}
}
async function writeLinks(categories: Category[]): Promise<void> {
try {
await writeFile(LINKS_FILE, JSON.stringify({ categories }, null, 2), {
encoding: "utf-8",
});
//console.log("Links updated successfully.");
} catch (error) {
console.error("Error writing links file:", error);
}
}
async function handler(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/") {
try {
const html = await Deno.readTextFile("./index.html");
return new Response(html, {
headers: { "Content-Type": "text/html" },
});
} catch (error) {
console.error("Error reading index.html:", error);
return new Response("Error loading the homepage.", { status: 500 });
}
} else if (url.pathname === "/style.css") {
try {
const css = await Deno.readTextFile(STYLE_FILE);
return new Response(css, {
headers: { "Content-Type": "text/css" },
});
} catch (error) {
console.error("Error reading style.css:", error);
return new Response("Error loading the stylesheet.", { status: 500 });
}
} else if (url.pathname === "/favicon.ico") {
try {
const favicon = await Deno.readFile("./favicon.ico");
return new Response(favicon, {
headers: { "Content-Type": "image/x-icon" },
});
} catch (error) {
console.error("Error reading favicon.ico:", error);
// It's common for browsers to request favicon.ico, so a 404 might be more appropriate
return new Response("Not found.", { status: 404 });
}
} else if (url.pathname === "/main.js") {
try {
const js = await Deno.readTextFile(JS_FILE);
return new Response(js, {
headers: { "Content-Type": "text/javascript" },
});
} catch (error) {
console.error("Error reading main.js:", error);
return new Response("Error loading the javascript.", { status: 500 });
}
} else if (url.pathname === "/api/links") {
if (request.method === "GET") {
const linksData = await readLinks();
return new Response(JSON.stringify(linksData), {
headers: { "Content-Type": "application/json" },
});
} else if (request.method === "POST") {
try {
const body = await request.json();
if (body && body.category && body.name && body.url) {
const { category, name, url } = body;
const linksData = await readLinks();
const categoryIndex = linksData.categories.findIndex((cat) => cat.name === category);
if (categoryIndex !== -1) {
if (!linksData.categories[categoryIndex].links) {
linksData.categories[categoryIndex].links = [];
}
linksData.categories[categoryIndex].links.push({ name, url });
await writeLinks(linksData.categories);
return new Response(JSON.stringify({ message: "Link added successfully" }), {
status: 201,
headers: { "Content-Type": "application/json" },
});
} else {
return new Response(`Category "${category}" not found.`, { status: 400 });
}
} else {
return new Response("Invalid request body.", { status: 400 });
}
} catch (error) {
console.error("Error processing POST request:", error);
return new Response("Error processing the request.", { status: 500 });
}
} else {
return new Response("Method not allowed.", { status: 405 });
}
} else if (url.pathname === "/api/categories") {
if (request.method === "POST") {
try {
const body = await request.json();
if (body && body.name) {
const newCategoryName = body.name.trim();
if (newCategoryName) {
const linksData = await readLinks();
const categoryExists = linksData.categories.some(
(cat) => cat.name.toLowerCase() === newCategoryName.toLowerCase()
);
if (!categoryExists) {
linksData.categories.push({ name: newCategoryName, links: [] });
await writeLinks(linksData.categories);
return new Response(JSON.stringify({ message: "Category added successfully" }), {
status: 201,
headers: { "Content-Type": "application/json" },
});
} else {
return new Response(`Category "${newCategoryName}" already exists.`, { status: 409 });
}
} else {
return new Response("Category name cannot be empty.", { status: 400 });
}
} else {
return new Response("Invalid request body. Missing 'name'.", { status: 400 });
}
} catch (error) {
console.error("Error processing POST request for /api/categories:", error);
return new Response("Error processing the request.", { status: 500 });
}
} else {
return new Response("Method not allowed.", { status: 405 });
}
} else if (url.pathname === "/api/reorder-links" && request.method === "POST") {
try {
const body = await request.json();
if (body && body.category && Array.isArray(body.order)) {
const { category, order } = body;
const linksData = await readLinks();
const categoryIndex = linksData.categories.findIndex((cat) => cat.name === category);
if (categoryIndex !== -1) {
// Create a new array of links based on the provided order
const orderedLinks: Link[] = [];
const existingLinks = linksData.categories[categoryIndex].links;
// Create a map of existing links for faster lookup (by name, assuming unique names for now)
const existingLinksMap = new Map(existingLinks.map(link => [link.name, link]));
// Iterate through the ordered names and find the corresponding link
for (const linkName of order) {
const foundLink = existingLinksMap.get(linkName);
if (foundLink) {
orderedLinks.push(foundLink);
}
}
// Update the category's links with the new order
linksData.categories[categoryIndex].links = orderedLinks;
await writeLinks(linksData.categories);
return new Response(JSON.stringify({ message: `Links in category "${category}" reordered successfully.` }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} else {
return new Response(`Category "${category}" not found.`, { status: 400 });
}
} else {
return new Response("Invalid request body. Missing 'category' or 'order'.", { status: 400 });
}
} catch (error) {
console.error("Error processing POST request for /api/reorder-links:", error);
return new Response("Error processing the request.", { status: 500 });
}
} else if (url.pathname === "/api/reorder-categories" && request.method === "POST") {
try {
const body = await request.json();
if (body && Array.isArray(body.order)) {
const { order } = body;
const linksData = await readLinks();
// Assuming the client-side sends { order: [...], oldName: "...", newName: "..." }
const oldName = body.oldName;
const newName = body.newName;
// Update the category name in the server data
if (oldName && newName) {
linksData.categories = linksData.categories.map(cat =>
cat.name === oldName ? { ...cat, name: newName } : cat
);
}
const orderedCategories: Category[] = [];
const existingCategoriesMap = new Map(linksData.categories.map(cat => [cat.name, cat]));
for (const categoryName of order) {
const foundCategory = existingCategoriesMap.get(categoryName);
if (foundCategory) {
orderedCategories.push(foundCategory);
}
}
linksData.categories = orderedCategories;
await writeLinks(linksData.categories);
return new Response(JSON.stringify({ message: "Categories reordered successfully." }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} else {
return new Response("Invalid request body. Missing 'order' array.", { status: 400 });
}
} catch (error) {
console.error("Error reordering categories:", error);
return new Response(JSON.stringify({ error: "Failed to reorder categories." }), { status: 500, headers: { "Content-Type": "application/json" } });
}
} else if (url.pathname === "/api/move-link" && request.method === "POST") {
try {
const body = await request.json();
if (body && body.linkName && body.sourceCategory && body.targetCategory) {
const { linkName, sourceCategory, targetCategory } = body;
const linksData = await readLinks();
let movedLink = null;
let sourceCategoryIndex = -1;
let targetCategoryIndex = -1;
// Find the link in the source category and store its data
linksData.categories = linksData.categories.map((cat, index) => {
if (cat.name === sourceCategory) {
sourceCategoryIndex = index;
cat.links = cat.links.filter(link => {
if (link.name === linkName) {
movedLink = link;
return false; // Remove the link from the source category
}
return true;
});
} else if (cat.name === targetCategory) {
targetCategoryIndex = index;
}
return cat;
});
// Add the link to the target category if found
if (movedLink && targetCategoryIndex !== -1) {
linksData.categories[targetCategoryIndex].links.push(movedLink);
await writeLinks(linksData.categories);
return new Response(JSON.stringify({ message: `Link "${linkName}" moved to "${targetCategory}".` }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} else if (!movedLink) {
return new Response(JSON.stringify({ error: `Link "${linkName}" not found in category "${sourceCategory}".` }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
} else {
return new Response(JSON.stringify({ error: `Target category "${targetCategory}" not found.` }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
}
} else {
return new Response("Invalid request body. Missing 'linkName', 'sourceCategory', or 'targetCategory'.", { status: 400 });
}
} catch (error) {
console.error("Error moving link:", error);
return new Response(JSON.stringify({ error: "Failed to move link." }), { status: 500, headers: { "Content-Type": "application/json" } });
}
} else {
return new Response("Not found.", { status: 404 });
}
}
console.log("Server listening on http://localhost:8083");
await serve(handler, { port: 8083 });