-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome-controller.ts
35 lines (30 loc) · 1.15 KB
/
home-controller.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
import { Headers, CompressionTypes, RequestMethod, ContentTypes } from "./constants.ts";
import Player from "./player/player.ts";
import Head from "./head.ts";
import Html from "./html.ts";
import { compressResponse } from "./utils.ts";
import { YT_API_KEY } from "./.env.json";
export default async function HomeController(request: Request): Promise<Response> {
let url = `https://www.googleapis.com/youtube/v3/videos?key=${YT_API_KEY}&part=snippet,statistics&chart=mostPopular`;
try {
const ytResponse = await fetch(url);
if (!ytResponse.ok) throw new Error("Response status: " + ytResponse.status);
const json = await ytResponse.json();
const html = Html({
head: Head({
pageTitle: "MyTube Home Page",
}),
body: Player(json),
})
const res: Response = compressResponse(html, request, {
headers: { [Headers.ContentType]: ContentTypes.Html }
});
return res;
} catch (error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
[Headers.ContentType]: ContentTypes.Html,
},
});
}
}