From 92e6e4404e92fb604effc98110111db1b42482b6 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 17 Apr 2021 10:38:45 +0200 Subject: Add version.txt endpoint --- .gitignore | 1 + LICENSE_NOTICES | 21 +++++++++++++++++++++ build_docker_image.sh | 31 +++++++++++++++++++++++++++++++ index.ts | 18 +++++++++++++----- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 LICENSE_NOTICES create mode 100755 build_docker_image.sh diff --git a/.gitignore b/.gitignore index 4c49bd7..188309e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +version.txt diff --git a/LICENSE_NOTICES b/LICENSE_NOTICES new file mode 100644 index 0000000..1805083 --- /dev/null +++ b/LICENSE_NOTICES @@ -0,0 +1,21 @@ +srht-git-feed uses third-party libraries or other resources that may +be distributed under licenses different than the srht-git-feed software. + +In the event that we accidentally failed to list a required notice, +please bring it to our attention through any of the ways detailed here: + + ivar@ivarlovlie.no + +The attached notices are provided for information only. + +For any licenses that require disclosure of source, sources are available at +https://git.sr.ht/~ivar/srht-git-feed + +License Notice for https://deno.land/std +--------------------------- +https://deno.land/std/LICENSE + + +License Notice for https://deno.land/x/dotenv +--------------------------- +https://deno.land/x/dotenv/LICENSE diff --git a/build_docker_image.sh b/build_docker_image.sh new file mode 100755 index 0000000..504dd4d --- /dev/null +++ b/build_docker_image.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +do_push() { + IMAGE_NAME="srht-git-feed" + HUB_NAME="ivarlovlie/srht-git-feed" + COMMIT_HASH=$(git rev-parse --short HEAD) + + echo $COMMIT_HASH >version.txt + docker build -t $IMAGE_NAME . + docker tag $IMAGE_NAME $HUB_NAME:$COMMIT_HASH + docker tag $IMAGE_NAME $HUB_NAME:latest + + echo "" + echo "CTRL+C to cancel, anything to push to docker hub" + read -n 1 + docker push $HUB_NAME:$COMMIT_HASH + docker push $HUB_NAME:latest +} + +if [ -z "$(git status --untracked-files=no --porcelain)" ]; then + do_push +else + echo "unclean git tree! CTRL+C to cancel, anything to commit and push to default branch" + read -n 1 + read -p "Enter commit message: " COMMIT_MESSAGE + git add . + git commit -m "$COMMIT_MESSAGE" + git push + do_push +fi diff --git a/index.ts b/index.ts index a3bb69c..5bf62c7 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import { serve } from "https://deno.land/std@0.93.0/http/server.ts"; +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"; @@ -59,11 +59,19 @@ async function getRepositories(): Promise { } } -const s = serve({ port: parseInt(SERVER_PORT), hostname: SERVER_HOST }); -console.log("http://"+SERVER_HOST+":" + SERVER_PORT); -for await (const req of s) { +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 { - req.respond({ body: JSON.stringify(await getRepositories()) }); + 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) }); } -- cgit v1.3