Last active
February 23, 2019 19:56
-
-
Save reyhansofian/9d9d8f7f242364595a56753bdeae721e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
const express = require("express"); | |
const app = express(); | |
const fs = require("fs"); | |
const bodyParser = require('body-parser'); | |
// volume mount path | |
const mountPath = "/etc/config"; | |
// ConfigMap filename | |
const configMapFile = "..data"; | |
// watching file | |
fs.watch(`${mountPath}`, (event, filename) => { | |
// only listen to event "rename". | |
// kubelet will rename the reference folder on ConfigMap update. | |
if (event === "rename" && filename) { | |
if (filename === configMapFile) { | |
console.log(`process.env BEFORE: ${JSON.stringify(process.env)}`); | |
// get all files from `..data` directory | |
const dir = fs.readdirSync(`${mountPath}/${configMapFile}`); | |
console.log(`Env list: ${dir}`); | |
// read all files inside `..data` directory | |
dir.forEach(env => { | |
// inject the new envar value to `process.env` object (not recommended) | |
process.env[env] = fs.readFileSync( | |
`${mountPath}/${configMapFile}/${env}` | |
); | |
}); | |
console.log(`process.env AFTER: ${JSON.stringify(process.env)}`); | |
} | |
} | |
}); | |
// for readinessProbe and livelinessProbe Kubernetes | |
app.get("/info", (req, res) => { | |
res.sendStatus(200); | |
}); | |
app.get("/isMaintenance", (req, res) => { | |
const isMaintenace = process.env.ENV_IS_MAINTENANCE; | |
res.status(200).send(`Is it maintenance? ${isMaintenace}`); | |
}) | |
app.listen(3000, err => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
console.log(`Server is up on port 3000`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment