-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
114 lines (101 loc) · 3.57 KB
/
server.ts
File metadata and controls
114 lines (101 loc) · 3.57 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
import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
import { initializeWorkers } from "./src/workerPool.ts";
import { initializeCache } from "./src/playerCache.ts";
import { handleDecryptSignature } from "./src/handlers/decryptSignature.ts";
import { handleGetSts } from "./src/handlers/getSts.ts";
import { handleResolveUrl } from "./src/handlers/resolveUrl.ts";
import { handleGetPoToken } from "./src/handlers/getPoToken.ts";
import { withMetrics } from "./src/middleware.ts";
import { withPlayer } from "./src/middleware/player.ts";
import { withValidation } from "./src/validation.ts";
import { registry } from "./src/metrics.ts";
import type { ApiRequest, RequestContext } from "./src/types.ts";
const API_TOKEN = Deno.env.get("API_TOKEN");
async function baseHandler(req: Request): Promise<Response> {
const { pathname } = new URL(req.url);
if (req.method === "GET") {
if (pathname === "/") {
try {
const file = await Deno.readFile("./docs/index.html");
return new Response(file, {
status: 200,
headers: { "Content-Type": "text/html" },
});
} catch {
return new Response(
"Homepage not found. Please refer to the API spec at https://github.com/kikkia/yt-cipher?tab=readme-ov-file#api-specification",
{
status: 404,
headers: { "Content-Type": "text/plain" },
},
);
}
}
if (pathname === "/swagger.yaml") {
try {
const file = await Deno.readFile("./docs/swagger.yaml");
return new Response(file, {
status: 200,
headers: { "Content-Type": "application/yaml" },
});
} catch {
return new Response("Swagger spec not found", {
status: 404,
headers: { "Content-Type": "text/plain" },
});
}
}
}
if (pathname === "/metrics") {
return new Response(registry.metrics(), {
headers: { "Content-Type": "text/plain" },
});
}
const authHeader = req.headers.get("authorization");
if (API_TOKEN && API_TOKEN !== "") {
if (authHeader !== API_TOKEN) {
const error = authHeader ? "Invalid API token" : "Missing API token";
return new Response(JSON.stringify({ error }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
}
let handle: (ctx: RequestContext) => Promise<Response>;
if (pathname === "/decrypt_signature") {
handle = handleDecryptSignature;
} else if (pathname === "/get_sts") {
handle = handleGetSts;
} else if (pathname === "/resolve_url") {
handle = handleResolveUrl;
} else if (pathname === "/get_pot") {
handle = handleGetPoToken;
} else {
return new Response(JSON.stringify({ error: "Not Found" }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
}
let body;
try {
body = (await req.json()) as ApiRequest;
} catch {
return new Response(JSON.stringify({ error: "Invalid JSON body" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
const ctx: RequestContext = { req, body };
const composedHandler =
pathname === "/get_pot"
? withValidation(withMetrics(handle))
: withValidation(withPlayer(withMetrics(handle)));
return await composedHandler(ctx);
}
const handler = baseHandler;
const port = Deno.env.get("PORT") || 8001;
const host = Deno.env.get("HOST") || "0.0.0.0";
await initializeCache();
initializeWorkers();
console.log(`Server listening on http://${host}:${port}`);
await serve(handler, { port: Number(port), hostname: host });