-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeatureManagement.js
More file actions
55 lines (47 loc) · 2.07 KB
/
featureManagement.js
File metadata and controls
55 lines (47 loc) · 2.07 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
50
51
52
53
54
55
// 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 AppConfig and FeatureManager instances
let appConfig;
let featureManager;
// Initialize AppConfig and FeatureManager
async function initializeFeatureManagement(appInsightsClient, targetingContextAccessor) {
console.log("Loading configuration...");
appConfig = await load(config.appConfigEndpoint, new DefaultAzureCredential(), {
featureFlagOptions: {
enabled: true,
selectors: [
{
keyFilter: "*"
}
],
refresh: {
enabled: true,
refreshIntervalInMs: 10_000
}
}
});
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
};