import { serve } from "https://deno.land/std/http/server.ts"; import "https://deno.land/x/dotenv/load.ts"; const SERVER_PORT = Deno.env.get("SERVER_PORT") ?? "8080"; const SERVER_HOST = Deno.env.get("SERVER_HOST") ?? "localhost"; const API_URL = Deno.env.get("API_URL") ?? "https://git.sr.ht/query"; const API_KEY = Deno.env.get("API_KEY"); interface QueryResponse { data: { repositories: { results: Repository[]; }; }; } interface Repository { id: string, name: string, description: string, updated: string, visibility: "PUBLIC" | "UNLISTED" | "PRIVATE" } window.onload = function() { if(!API_KEY) { throw new Error("API_KEY is empty"); } } async function getRepositories(): Promise { const response = await fetch(API_URL, { method: "POST", headers: { "Authorization": "Bearer " + API_KEY, "Content-Type": "application/json" }, body: JSON.stringify({ query: ` { repositories { results { id, name, description, updated, visibility } } } ` }) }) if (response.ok) { const json = await response.json() as QueryResponse; return json?.data.repositories.results?.filter(repo => repo.visibility === "PUBLIC") ?? []; } else { throw response; } } console.log("srht-git-feed is running on: http://" + SERVER_HOST + ":" + SERVER_PORT); for await (const req of serve({ port: parseInt(SERVER_PORT), hostname: SERVER_HOST })) { try { if (req.url === "/version.txt") { req.respond({ body: await Deno.readTextFile("version.txt") }); } else if (req.url === "/") { req.respond({ body: JSON.stringify(await getRepositories()) }); } else { req.respond({ status: 404 }); } } catch(err) { req.respond({ body: JSON.stringify(err) }); } }