Skip to content

Commit

Permalink
Export /data API with the ENABLE_DATA_API env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenasm committed Apr 16, 2024
1 parent 1cd72eb commit 6c4e1d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ List of environment variables for more customization:
| NODE_EXPORTER_PORT | 9100 | |
| CADVISOR_SERVICE_NAME_REGEX | cadvisor | Use this env to enable `cadvisor` integration. |
| CADVISOR_PORT | 8080 | |
| ENABLE_DATA_API | true | Use this env to export the `/data` API that returns the swarm status as a JSON object. Note that it requires basic-auth if `ENABLE_AUTHENTICATION` is activated. |


## Security
Expand Down
32 changes: 21 additions & 11 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const dockerUpdateInterval = parseInt(process.env.DOCKER_UPDATE_INTERVAL || "500
const metricsUpdateInterval = parseInt(process.env.METRICS_UPDATE_INTERVAL || "30000");
const showTaskTimestamp = !(process.env.SHOW_TASK_TIMESTAMP === "false");
const debugMode = process.env.DEBUG_MODE === "true";
const enableDataAPI = process.env.ENABLE_DATA_API === "true";

const _nodeExporterServiceNameRegex = process.env.NODE_EXPORTER_SERVICE_NAME_REGEX || "";
const useNodeExporter = _nodeExporterServiceNameRegex !== "";
Expand Down Expand Up @@ -532,6 +533,16 @@ const dropClosed = listeners => {

// set up the application

let lastRunningNodeExportes = [];
let lastNodeMetrics = [];
let lastRunningCadvisors = [];
let lastRunningTasksID = [];
let lastRunningTasksMetrics = [];

let listeners = [];
let lastData = {};
let lastSha = '';

users = {};
users[username] = password;
const basicAuthConfig = () => basicAuth({
Expand All @@ -551,10 +562,20 @@ if (enableAuthentication) {
tokenStore.add(token);
res.send(token);
});
if (enableDataAPI) {
router.get('/data', basicAuthConfig(), (req, res) => {
res.send(lastData);
});
}
} else {
router.get('/auth_token', (req, res) => {
res.send("no-auth-token-needed");
});
if (enableDataAPI) {
router.get('/data', (req, res) => {
res.send(lastData);
});
}
}
if (debugMode) {
console.log("debug mode is active");
Expand All @@ -572,17 +593,6 @@ if (debugMode) {
app.use(pathPrefix + "/", router);

// start the polling

let lastRunningNodeExportes = [];
let lastNodeMetrics = [];
let lastRunningCadvisors = [];
let lastRunningTasksID = [];
let lastRunningTasksMetrics = [];

let listeners = [];
let lastData = {};
let lastSha = '';

setInterval(() => { // update docker data
fetchDockerData()
.then(it => {
Expand Down

0 comments on commit 6c4e1d7

Please sign in to comment.