Skip to content

Commit

Permalink
feat: adding request header x-api-version to have package.json version
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2sw committed May 17, 2022
1 parent 2a41ca0 commit 4399212
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scheduler from '../tasks';
import { V1Router } from './v1';
import { sequelize } from '../database';
import { getConfig } from '../utils/config-loader';
import { fileLoader } from '../utils/file-loader';
import { logger } from '../config/logger.cjs';
import {
assertChiaNetworkMatchInConfiguration,
Expand Down Expand Up @@ -67,6 +68,14 @@ app.use(function (req, res, next) {
next();
});

app.use(function (req, res, next) {
const packageJson = fileLoader('package.json');
logger.debug(`Setting header x-api-verion to package.json version: ${packageJson.version}`);
res.setHeader('x-api-version', packageJson.version);

next();
});

app.use('/v1', V1Router);

sequelize.authenticate().then(async () => {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/file-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _ from 'lodash';
import yaml from 'js-yaml';
import fs from 'fs';
import path from 'path';

import { logger } from '../config/logger.cjs';

export const fileLoader = _.memoize((filepath) => {
logger.debug(`Reading file at ${filepath}`);

const file = path.resolve(filepath);

try {
return yaml.load(fs.readFileSync(file, 'utf8'));
} catch (e) {
logger.error(`File not found at ${file}`, e);
}
});

0 comments on commit 4399212

Please sign in to comment.