Last active
February 24, 2019 11:07
-
-
Save reyhansofian/7d8db68335870d5a889fabbd5e221413 to your computer and use it in GitHub Desktop.
Application server using configmap reload
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'); | |
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