forked from PeronGH/ebridge-timetable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
45 lines (35 loc) · 1.14 KB
/
server.ts
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
import { Application, Router } from "./deps.ts";
import { DOMParser, genCalendar, parseTimetable } from "./mod.ts";
const app = new Application();
const router = new Router();
router.post("/calendar-parse", async (ctx) => {
const body = ctx.request.body();
if (body.type !== "text") {
ctx.response.status = 400;
ctx.response.body = { error: "请求体必须是文本格式的HTML" };
return;
}
const htmlContent = await body.value;
const parser = new DOMParser();
const document = parser.parseFromString(htmlContent, "text/html");
if (!document) {
ctx.response.status = 400;
ctx.response.body = { error: "无法解析HTML内容" };
return;
}
try {
const lessons = parseTimetable(document);
const calendar = genCalendar(lessons);
ctx.response.body = {
lessons: lessons,
ics: calendar.toString(),
};
} catch (error) {
ctx.response.status = 500;
ctx.response.body = { error: "处理课程表时出错: " + error.message };
}
});
app.use(router.routes());
app.use(router.allowedMethods());
console.log("服务器运行在 http://localhost:8000");
await app.listen({ port: 8000 });