Skip to content

Commit

Permalink
feat: adds status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
migtarx committed Dec 6, 2023
1 parent d0e11f3 commit 5593c59
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 3 deletions.
170 changes: 169 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
"main": "app.js",
"scripts": {
"start": "node ./src/app.js",
"dev": "nodemon ./src/app.js"
"dev": "nodemon ./src/app.js",
"start:pro": "npm start -- --mode=pro"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongoose": "^8.0.0"
"mongoose": "^8.0.0",
"pjson": "^1.0.9",
"yargs": "^17.7.2"
}
}
14 changes: 14 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require('dotenv').config();
const express = require('express');
const path = require('path');
const pjson = require('pjson');
const app = express();
const db = require('./database/connection.js');
const BootService = require('./services/boot.service');
const execMode = BootService.getExecutionMode();
const PORT = process.env.PORT || 3000;
const Static = require("./models/Static");
let globalViews = 0;
Expand All @@ -11,6 +14,17 @@ db.then(() => console.log(`Connected to mongodb server`)).catch((err) =>
console.log(`mongodb connection failed: ${err}`),
);

app.get('/status', (req, res) => {
res.json({
Status: 'OK',
'Runtime-Mode': execMode == 'pro' ? 'Production' : 'Development',
'Application-Version': pjson.version,
'Application-Description': 'migtarx.com BLOG',
'Application-Author': '[email protected]',
'Application-Owner': 'Miguel Puerta',
});
});

app.get('/views', (req, res) => {
res.send(globalViews.toString());
});
Expand Down
22 changes: 22 additions & 0 deletions src/services/boot.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const argv = require('yargs').argv;

global.exec_mode = 'dev';

if (argv.mode) {
const modeValue = argv.mode;
const allowedModes = ['dev', 'pro'];

if (allowedModes.includes(modeValue)) {
global.exec_mode = modeValue;
} else {
console.log('Invalid mode. Allowed modes are: dev, pro, uat. Running in dev mode.');
}
}

function getExecutionMode() {
return global.exec_mode;
}

module.exports = {
getExecutionMode,
};

0 comments on commit 5593c59

Please sign in to comment.