Skip to content

Instantly share code, notes, and snippets.

@jrbarron
Created February 12, 2020 08:08
Show Gist options
  • Save jrbarron/742799ef8a202996619ec2085366cbf8 to your computer and use it in GitHub Desktop.
Save jrbarron/742799ef8a202996619ec2085366cbf8 to your computer and use it in GitHub Desktop.
Toggles on startup
// ==========================
// config.js
// ==========================
{
"version": 1,
"strategies": [
{
"name": "organizationId",
"description": "Active for a specific list of organization ids",
"parameters": [
{
"name": "ids",
"type": "list",
"description": "List of organization ids that should have the feature activated",
"required": true
}
]
}
]
}
// ==========================
// server.js
// ==========================
const configFile = './config.json';
const saveConfig = async (stateService) => {
const exportedData = await stateService.export({
includeStrategies: true,
includeFeatureToggles: false,
});
fs.readFile(configFile, (err, data) => {
const configJson = JSON.stringify(exportedData, null, 2);
if (data.toString() !== configJson) {
logger.info('Updating config');
fs.writeFile(configFile, configJson);
}
});
};
const startPoll = (stateService) => {
setInterval(async () => {
await saveConfig(stateService);
}, 10000);
};
unleash.start({
databaseUrl: `postgres://${PG_USERNAME}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/${PG_DATABASE}`,
port: PORT,
getLogger,
importFile: configFile,
}).then(async ({ stateService }) => {
// You can enable this when developing locally such that config.json is updated
// startPoll(stateService);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment