Skip to content

Commit

Permalink
Merge pull request #17 from rcruzper/refactor-info
Browse files Browse the repository at this point in the history
refactor info endpoint
  • Loading branch information
rcruzper authored Aug 29, 2019
2 parents 3d02218 + 83bf7be commit 1ff8346
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 643 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ app.use(actuator('/management')); // It will set /management/info instead of /in
}
```
IMPORTANT: To get this information the middleware have some sort of logic:
1. If the express app is executed with ```npm start``` it will get the data from process.env
2. If the express app is executed with ```node app.js``` the module will look for a file named package.json where the node command was launched.
3. Git information will show only if exists a ```git.properties``` file where the app was launched. You can use [node-git-info](https://www.npmjs.com/package/node-git-info) to generate this file.
1. When the express app is executed with ```node app.js``` or ```npm start``` the module will look for a file named package.json where the node command was launched.
2. Git information will show only if exists a ```git.properties``` file where the app was launched. You can use [node-git-info](https://www.npmjs.com/package/node-git-info) to generate this file.

### metrics
```json
Expand Down
61 changes: 35 additions & 26 deletions lib/infoRoute.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
'use strict';

var fs = require('fs');
var read = require('utils-fs-read-properties');
var moment = require('moment');

// Just for rewire this variable in unit tests
var envPackageName = process.env.npm_package_name;
const fs = require('fs');
const properties = require('utils-fs-read-properties');
const moment = require('moment');

module.exports = function info(req, res) {
if (envPackageName != null) {
var packageName = process.env.npm_package_name;
var packageDescription = process.env.npm_package_description;
var packageVersion = process.env.npm_package_version;
const build = getBuild();
const git = getGit();

res.status(200).json({
build: build,
git: git
});
};

function getBuild() {
let packageJson;
try {
const packageFile = fs.readFileSync('./package.json', 'utf8');
packageJson = JSON.parse(packageFile);
} catch (err) {
// Error getting and parsing package.json
}
else {
var pkgjson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

packageName = pkgjson.name;
packageDescription = pkgjson.description;
packageVersion = pkgjson.version;
let build;
if (packageJson !== undefined) {
build = {
name: packageJson.name,
description: packageJson.description,
version: packageJson.version
}
}

var data = read.sync('git.properties');
var git;
return build;
}

function getGit() {
let git;

const data = properties.sync('git.properties');
if (!(data instanceof Error)) {
git = {
branch: data['git.branch'],
Expand All @@ -33,12 +49,5 @@ module.exports = function info(req, res) {
}
}

res.status(200).json({
build: {
name: packageName,
description: packageDescription,
version: packageVersion
},
git: git
});
};
return git;
}
Loading

0 comments on commit 1ff8346

Please sign in to comment.