-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeatureManagement.js
More file actions
49 lines (41 loc) · 1.98 KB
/
featureManagement.js
File metadata and controls
49 lines (41 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
const { DefaultAzureCredential } = require("@azure/identity");
const { load } = require("@azure/app-configuration-provider");
const { FeatureManager, ConfigurationMapFeatureFlagProvider } = require("@microsoft/feature-management");
const { createTelemetryPublisher } = require("@microsoft/feature-management-applicationinsights-node");
const config = require("./config");
// Variables to hold the App Configuration provider and feature manager instances
let appConfig;
let featureManager;
// Initialize App Configuration provider and feature management
async function initializeFeatureManagement(appInsightsClient, targetingContextAccessor) {
console.log("Loading feature flags from Azure App Configuration...");
appConfig = await load(config.appConfigEndpoint, new DefaultAzureCredential(), {
featureFlagOptions: {
enabled: true,
refresh: {
enabled: true
}
}
});
const featureFlagProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
const publishTelemetry = createTelemetryPublisher(appInsightsClient);
featureManager = new FeatureManager(featureFlagProvider, {
onFeatureEvaluated: publishTelemetry,
targetingContextAccessor: targetingContextAccessor
});
return { featureManager, appConfig };
}
// Middleware to refresh configuration before each request
const featureFlagRefreshMiddleware = (req, res, next) => {
// The configuration refresh happens asynchronously to the processing of your app's incoming requests.
// It will not block or slow down the incoming request that triggered the refresh.
// The request that triggered the refresh may not get the updated configuration values, but later requests will get new configuration values.
appConfig?.refresh(); // intended to not await the refresh
next();
};
module.exports = {
initializeFeatureManagement,
featureFlagRefreshMiddleware
};