-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose commit hash and version in the about service (#27)
* Expose commit hash and version in the about service * Fix paths
- Loading branch information
Showing
3 changed files
with
33 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
import { log } from 'console'; | ||
import { FastifyPluginAsync } from 'fastify'; | ||
import { version } from '../../../../../../package.json'; | ||
|
||
import { readFileSync } from 'fs'; | ||
const GIT_COMMIT_HASH_FILE = 'git-commit-hash.txt'; | ||
const VERSION = process.env.VERSION || 'UNKNOWN, please set the environment variable'; | ||
const COMMIT_HASH = getCommitHash(); | ||
import { join } from 'path'; | ||
|
||
|
||
|
||
interface AboutResponse { | ||
name: string; | ||
version: string; | ||
gitCommitHash?: string | undefined; | ||
} | ||
|
||
const example: FastifyPluginAsync = async (fastify): Promise<void> => { | ||
fastify.get<{ Reply: AboutResponse }>('/', async function (_request, reply) { | ||
return reply.send({ | ||
name: 'BFF API', | ||
version, | ||
version: VERSION, | ||
gitCommitHash: COMMIT_HASH, | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Read a file with the git commit hash (generated for example using github actions) | ||
*/ | ||
function getCommitHash(): string | undefined { | ||
const filePath = join(__dirname, '../../../../../../../../', GIT_COMMIT_HASH_FILE) | ||
try { | ||
return readFileSync(filePath, 'utf-8') | ||
} catch (error) { | ||
// Not a big deal, if the file is not present, the about won't | ||
console.warn(`Unable to read the commit hash file ${filePath}, therefore won't be exported in the about endpoint`); | ||
return undefined | ||
} | ||
} | ||
|
||
export default example; |