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" },
});
}