-
Notifications
You must be signed in to change notification settings - Fork 0
/
saas.ts
76 lines (67 loc) · 2.01 KB
/
saas.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
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
import { days } from "./days/mod.ts";
import denoJson from "./deno.json" with { type: "json" };
import { getLines } from "./utils/getLines.ts";
export default {
async fetch(req) {
const url = new URL(req.url);
url.pathname = url.pathname.replace(/\/$/, "");
console.debug(`Request for ${url.pathname}`);
if (url.pathname === "/info") {
return new Response(
json({
name: "Advent of Code 2024",
description: "Solutions to Advent of Code 2024",
version: denoJson.version,
author: "spalberg",
source: "https://github.com/spalberg/advent-of-code-2024",
}),
jsonHeaders,
);
}
if (url.pathname === "/days") {
return new Response(
json({
days: Array.from(days.keys()),
}),
jsonHeaders,
);
}
const match = daysPattern.exec(url);
if (match != null) {
if (req.method !== "POST") {
return new Response(json({ error: "only POST shall pass" }), {
status: 405,
...jsonHeaders,
});
}
const input = req.body != null ? await getLines(req.body) : [];
if (input.length === 0) {
return new Response(
json({ error: "you need to send some input data" }),
{
status: 422,
...jsonHeaders,
},
);
}
const num = parseInt(match.pathname.groups.num ?? "", 10);
const day = days.get(num);
if (day == null) {
return new Response(null, { status: 404 });
}
console.log(`Processing day ${num}`);
return new Response(
json({ day: num, part1: day.part1(input), part2: day.part2(input) }),
jsonHeaders,
);
}
return new Response(null, { status: 404 });
},
} satisfies Deno.ServeDefaultExport;
const daysPattern = new URLPattern({ pathname: "/days/:num" });
const jsonHeaders = {
headers: { "Content-Type": "application/json" },
};
function json(data: unknown) {
return JSON.stringify(data, null, 2);
}