From 0a8c0885cb6751df116adaaa80c431c7c8e8941c Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Fri, 16 Apr 2021 21:16:29 +0200 Subject: Initial commit --- .gitignore | 1 + .vscode/settings.json | 5 ++++ Dockerfile | 5 ++++ LICENSE | 23 +++++++++++++++++ index.ts | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 index.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2c7ddc5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.unstable": true +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8e29e19 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM hayd/alpine-deno:1.5.2 +USER deno +COPY index.ts . +ENTRYPOINT ["deno"] +CMD ["run", "--allow-net", "--allow-env", "--allow-read", "index.ts"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f9a22b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) Ivar Løvlie. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..a3bb69c --- /dev/null +++ b/index.ts @@ -0,0 +1,70 @@ +import { serve } from "https://deno.land/std@0.93.0/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; + } +} + +const s = serve({ port: parseInt(SERVER_PORT), hostname: SERVER_HOST }); +console.log("http://"+SERVER_HOST+":" + SERVER_PORT); +for await (const req of s) { + try { + req.respond({ body: JSON.stringify(await getRepositories()) }); + } catch(err) { + req.respond({ body: JSON.stringify(err) }); + } +} -- cgit v1.3