From e55a0b2b770797d97a9fd87a0673e0bea0dfb27b Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Sat, 27 Jan 2024 21:31:32 +0100 Subject: [PATCH] feat: add version API endpoint --- package.json | 1 + src/api/v3/index.js | 11 +++++++++++ src/index.js | 5 +++++ yarn.lock | 5 +++++ 4 files changed, 22 insertions(+) diff --git a/package.json b/package.json index f5ee0e2..2d17a20 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ }, "devDependencies": { "@biomejs/biome": "1.2.2", + "@richex/git-last-commit": "^2.1.1", "@vercel/ncc": "^0.34.0", "chai": "^4.3.6", "copy-webpack-plugin": "^11.0.0", diff --git a/src/api/v3/index.js b/src/api/v3/index.js index 80a0d28..73da0c2 100644 --- a/src/api/v3/index.js +++ b/src/api/v3/index.js @@ -30,9 +30,20 @@ more information on this, and how to apply and follow the GNU AGPL, see { + // TODO Support responding with JSON object when requested + + const versionDetails = (app.get('git_branch') && app.get('git_hash')) + ? `+${app.get('git_branch')}.${app.get('git_hash').substring(0, 10)}` + : '' + + return res.status(200).contentType('text/plain').send(`${app.get('keyoxide_name')}/${app.get('keyoxide_version')}${versionDetails}`) +}) + if ((process.env.ENABLE_MAIN_MODULE ?? 'true') === 'true') { router.use('/profile', keyoxideProfileApiRouter) } diff --git a/src/index.js b/src/index.js index f583006..8c88dad 100644 --- a/src/index.js +++ b/src/index.js @@ -27,6 +27,7 @@ You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see . */ +import git from '@richex/git-last-commit' import express from 'express' import * as httpContext from 'express-http-context2' import { nanoid } from 'nanoid' @@ -45,13 +46,17 @@ dotenv.config() const app = express() const packageData = JSON.parse(readFileSync('./package.json')) +const commit = await git.getLastCommitAsync() app.set('env', process.env.NODE_ENV || 'production') app.engine('pug', pug.__express).set('view engine', 'pug') app.set('port', process.env.PORT || 3000) app.set('domain', process.env.DOMAIN) app.set('scheme', process.env.SCHEME || 'https') +app.set('keyoxide_name', 'keyoxide-web') app.set('keyoxide_version', packageData.version) +app.set('git_branch', commit.branch ?? process.env.CI_COMMIT_BRANCH ?? process.env.COMMIT_BRANCH) +app.set('git_hash', commit.hash ?? process.env.CI_COMMIT_SHA ?? process.env.COMMIT_SHA) app.set('onion_url', process.env.ONION_URL) // Middlewares diff --git a/yarn.lock b/yarn.lock index f84820c..59cd22c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -488,6 +488,11 @@ resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.24.tgz#58601079e11784d20f82d0585865bb42305c4df3" integrity sha512-2LuNTFBIO0m7kKIQvvPHN6UE63VjpmL9rnEEaOOaiSPbZK+zUOYIzBAWcED+3XYzhYsd/0mD57VdxAEqqV52CQ== +"@richex/git-last-commit@^2.1.1": + version "2.1.1" + resolved "https://registry.yarnpkg.com/@richex/git-last-commit/-/git-last-commit-2.1.1.tgz#b6527ae4223af8143f13e39d8f0cdaeaed7f9ba6" + integrity sha512-rNa0hCMyKTJ+qPwyN54FlwTUhDBNNghiQWt0ES27g11iYzyzpLcFBGLKYhDaEEEUPZ7aawXOCEYAwCg5JZ1FAQ== + "@sindresorhus/is@^4.0.0": version "4.6.0" resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-4.6.0.tgz#3c7c9c46e678feefe7a2e5bb609d3dbd665ffb3f"