feat: add version API endpoint

This commit is contained in:
Yarmo Mackenbach 2024-01-27 21:31:32 +01:00
parent 0061149aa5
commit e55a0b2b77
No known key found for this signature in database
GPG key ID: C248C28D432560ED
4 changed files with 22 additions and 0 deletions

View file

@ -29,6 +29,7 @@
}, },
"devDependencies": { "devDependencies": {
"@biomejs/biome": "1.2.2", "@biomejs/biome": "1.2.2",
"@richex/git-last-commit": "^2.1.1",
"@vercel/ncc": "^0.34.0", "@vercel/ncc": "^0.34.0",
"chai": "^4.3.6", "chai": "^4.3.6",
"copy-webpack-plugin": "^11.0.0", "copy-webpack-plugin": "^11.0.0",

View file

@ -30,9 +30,20 @@ more information on this, and how to apply and follow the GNU AGPL, see <https:/
import express from 'express' import express from 'express'
import keyoxideProfileApiRouter from './keyoxide_profile.js' import keyoxideProfileApiRouter from './keyoxide_profile.js'
import proxyGetApiRouter from './proxy_get.js' import proxyGetApiRouter from './proxy_get.js'
import app from '../../index.js'
const router = express.Router() const router = express.Router()
router.get('/version', async (req, res) => {
// 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') { if ((process.env.ENABLE_MAIN_MODULE ?? 'true') === 'true') {
router.use('/profile', keyoxideProfileApiRouter) router.use('/profile', keyoxideProfileApiRouter)
} }

View file

@ -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 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 <https://www.gnu.org/licenses/>. more information on this, and how to apply and follow the GNU AGPL, see <https://www.gnu.org/licenses/>.
*/ */
import git from '@richex/git-last-commit'
import express from 'express' import express from 'express'
import * as httpContext from 'express-http-context2' import * as httpContext from 'express-http-context2'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
@ -45,13 +46,17 @@ dotenv.config()
const app = express() const app = express()
const packageData = JSON.parse(readFileSync('./package.json')) const packageData = JSON.parse(readFileSync('./package.json'))
const commit = await git.getLastCommitAsync()
app.set('env', process.env.NODE_ENV || 'production') app.set('env', process.env.NODE_ENV || 'production')
app.engine('pug', pug.__express).set('view engine', 'pug') app.engine('pug', pug.__express).set('view engine', 'pug')
app.set('port', process.env.PORT || 3000) app.set('port', process.env.PORT || 3000)
app.set('domain', process.env.DOMAIN) app.set('domain', process.env.DOMAIN)
app.set('scheme', process.env.SCHEME || 'https') app.set('scheme', process.env.SCHEME || 'https')
app.set('keyoxide_name', 'keyoxide-web')
app.set('keyoxide_version', packageData.version) 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) app.set('onion_url', process.env.ONION_URL)
// Middlewares // Middlewares

View file

@ -488,6 +488,11 @@
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.24.tgz#58601079e11784d20f82d0585865bb42305c4df3" resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.24.tgz#58601079e11784d20f82d0585865bb42305c4df3"
integrity sha512-2LuNTFBIO0m7kKIQvvPHN6UE63VjpmL9rnEEaOOaiSPbZK+zUOYIzBAWcED+3XYzhYsd/0mD57VdxAEqqV52CQ== 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": "@sindresorhus/is@^4.0.0":
version "4.6.0" version "4.6.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-4.6.0.tgz#3c7c9c46e678feefe7a2e5bb609d3dbd665ffb3f" resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-4.6.0.tgz#3c7c9c46e678feefe7a2e5bb609d3dbd665ffb3f"