Skip to content

Commit

Permalink
refactor: extracts status routes to single file
Browse files Browse the repository at this point in the history
  • Loading branch information
migtarx committed Aug 18, 2024
1 parent 48d6683 commit 5e9f739
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require('dotenv').config();
const express = require('express');
const path = require('path');
const pjson = require('pjson');
const cookieParser = require("cookie-parser");
const helmet = require('helmet');
const app = express();
const logger = require('./middlewares/logger');
const { launchLog } = require("./utils/logger.js")
const { statusRoute } = require('./routes/routes.js');
const PORT = process.env.PORT || 3000;

app.use(express.json())
Expand All @@ -15,17 +15,7 @@ app.use(helmet({
contentSecurityPolicy: false
}));
app.use(logger);

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

app.use(express.static(path.join(__dirname, 'public')));

Expand Down
5 changes: 5 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const statusController = require('./status.controller');

module.exports = {
statusController
};
30 changes: 30 additions & 0 deletions src/controllers/status.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require('dotenv').config();
const pjson = require('pjson');
const execMode = process.env.ENV;

module.exports = {
getStatus,
getStatusHeartBeat
};

function getServerStatusData(isHeartBeat = false){
const statusInformation = {
Status: 'OK',
'Runtime-Mode': execMode == 'prod' ? 'Production' : 'Development',
'Application-Description': 'migtarx.com BLOG',
'Application-Author': '[email protected]',
'Application-Owner': 'Miguel Puerta',
}
if (!isHeartBeat) {
statusInformation['Application-Version'] = pjson.version;
}
return statusInformation;
}

function getStatus(req, res) {
return res.json(getServerStatusData());
}

function getStatusHeartBeat(req, res) {
return res.json(getServerStatusData(true));
}
5 changes: 5 additions & 0 deletions src/routes/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const statusRoute = require('./status.route.js');

module.exports = {
statusRoute
};
9 changes: 9 additions & 0 deletions src/routes/status.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const statusRouter = express.Router();
const { statusController } = require('../controllers');

statusRouter.get('/', statusController.getStatus);

statusRouter.get('/heartbeat', statusController.getStatusHeartBeat);

module.exports = statusRouter;

0 comments on commit 5e9f739

Please sign in to comment.