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
export default async function server(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/files") {
const user = url.searchParams.get("user");
const repo = url.searchParams.get("repo");
if (!user || !repo) {
return new Response("Missing user or repo parameter", { status: 400 });
}
const apiUrl = `https://api.github.com/repos/${user}/${repo}/contents`;
const response = await fetch(apiUrl);
const contents = await response.json();
let fileList = "";
if (Array.isArray(contents)) {
fileList = contents.map(file => `<li>${file.name} (${file.type})</li>`).join("");
} else {
fileList = "<li>Error: Unable to fetch repository contents</li>";
}
return new Response(`
<h2>Files in ${user}/${repo}:</h2>
<ul>${fileList}</ul>
`, {
headers: { "Content-Type": "text/html" },
});
}
return new Response(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Repository File Viewer</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
input, button { margin: 5px; padding: 5px; }
#result { margin-top: 20px; }
</style>
</head>
<body>
<h1>GitHub Repository File Viewer</h1>
<input type="text" id="user" placeholder="GitHub Username">
<input type="text" id="repo" placeholder="Repository Name">
<button onclick="fetchFiles()">Fetch Files</button>
<div id="result"></div>
<script>
async function fetchFiles() {
const user = document.getElementById('user').value;
const repo = document.getElementById('repo').value;
const result = document.getElementById('result');
result.innerHTML = 'Loading...';
const response = await fetch(\`/api/files?user=\${user}&repo=\${repo}\`);
const html = await response.text();
result.innerHTML = html;
}
</script>
</body>
</html>
`, {
headers: { "Content-Type": "text/html" },
});
}