From a79a1eab813891d6b9a53f1ee06511fc0b1c969c Mon Sep 17 00:00:00 2001 From: grey Date: Mon, 10 Jul 2023 15:09:30 +0200 Subject: [PATCH] improve /version api route to only call git once on startup --- src/index.ts | 4 ++++ src/routes/api/v1/version.ts | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index d60da14..b86bf37 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,6 +67,10 @@ Sentry.init({ environment: config.global.debug ? 'development' : 'production' }); +app.locals.versionRev = require('child_process') +.execSync('git rev-parse --short HEAD') +.toString().trim() + // RequestHandler creates a separate execution context using domains, so that every // transaction/span/breadcrumb is attached to its own Hub instance app.use(Sentry.Handlers.requestHandler()); diff --git a/src/routes/api/v1/version.ts b/src/routes/api/v1/version.ts index 4e75092..52b285b 100644 --- a/src/routes/api/v1/version.ts +++ b/src/routes/api/v1/version.ts @@ -1,9 +1,7 @@ import express, { Request, Response } from 'express'; function get(req: Request, res: Response) { - const revision = require('child_process') - .execSync('git rev-parse --short HEAD') - .toString().trim() + const revision = req.app.locals.versionRev; res.status(200).send({ version: '1.0.0', commit: revision }); };