-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.mjs
More file actions
65 lines (52 loc) · 1.91 KB
/
server.mjs
File metadata and controls
65 lines (52 loc) · 1.91 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
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import express from "express";
import { load } from "@azure/app-configuration-provider";
const connectionString = "<your-connection-string>";
const appConfig = await load(connectionString, {
featureFlagOptions: {
enabled: true,
selectors: [{
keyFilter: "*"
}],
refresh: {
enabled: true
}
}
});
appConfig.onRefresh(() => {
console.log("Configuration has been refreshed.");
});
import { ConfigurationObjectFeatureFlagProvider, ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";
/*
You can use either ConfigurationObjectFeatureFlagProvider or ConfigurationMapFeatureFlagProvider to provide feature flags.
We recommend using Azure App Configuration as the source of feature flags.
*/
// import path from "path";
// const config = JSON.parse(await fs.readFile("config.json"));
// const featureProvider = new ConfigurationObjectFeatureFlagProvider(config);
const featureProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
const featureManager = new FeatureManager(featureProvider);
const server = express();
const PORT = 3000;
// Use a middleware to achieve request-driven configuration refresh
server.use((req, res, next) => {
// this call is not blocking, the configuration will be updated asynchronously
appConfig.refresh();
next();
})
server.get("/", (req, res) => {
res.send("Hello World!");
});
server.get("/Beta", async (req, res) => {
const { userId, groups } = req.query;
if (await featureManager.isEnabled("Beta", { userId: userId, groups: groups ? groups.split(",") : [] })) {
res.send("Welcome to the Beta page!");
} else {
res.status(404).send("Page not found");
}
});
// Start the server
server.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});