Skip to content

Instantly share code, notes, and snippets.

@reyhansofian
Last active February 24, 2019 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyhansofian/7d8db68335870d5a889fabbd5e221413 to your computer and use it in GitHub Desktop.
Save reyhansofian/7d8db68335870d5a889fabbd5e221413 to your computer and use it in GitHub Desktop.
Application server using configmap reload
// @ts-check
const express = require("express");
const app = express();
const fs = require("fs");
const bodyParser = require('body-parser');
const mountPath = "/etc/config";
const configMapFile = "..data";
function readConfig() {
const dir = fs.readdirSync(`${mountPath}/${configMapFile}`);
console.log(`Available envar: ${dir}`);
dir.forEach(env => {
process.env[env] = fs.readFileSync(
`${mountPath}/${configMapFile}/${env}`
);
});
}
app.get("/info", (req, res) => {
res.sendStatus(200);
});
app.use(bodyParser.json());
// configmap reload webhook
app.post("/-/reload", (req, res) => {
// read new ConfigMap value
readConfigMap();
console.log(`process.env AFTER: ${JSON.stringify(process.env)}`);
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