Skip to content

Commit 7db4c1d

Browse files
add server example
1 parent d8c22c4 commit 7db4c1d

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

examples/express-app/config.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"feature_management": {
3+
"feature_flags": [
4+
{
5+
"id": "Beta",
6+
"enabled": true,
7+
"conditions": {
8+
"client_filters": [
9+
{
10+
"name": "Microsoft.Targeting",
11+
"parameters": {
12+
"Audience": {
13+
"Users": [
14+
"Jeff"
15+
],
16+
"Groups": [
17+
{
18+
"Name": "Admin",
19+
"RolloutPercentage": 100
20+
}
21+
],
22+
"DefaultRolloutPercentage": 40
23+
}
24+
}
25+
}
26+
]
27+
}
28+
}
29+
]
30+
}
31+
}

examples/express-app/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"@azure/app-configuration-provider": "latest",
4+
"@microsoft/feature-management": "latest",
5+
"express": "^4.21.2"
6+
}
7+
}

examples/express-app/pages/beta.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Beta</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
text-align: center;
10+
background-color: #ffffff;
11+
color: #000000;
12+
display: flex;
13+
justify-content: center;
14+
align-items: center;
15+
height: 75vh;
16+
}
17+
h1 {
18+
font-size: 5em;
19+
margin: 0;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<div>
25+
<h1>Welcome to the Beta page!</h1>
26+
</div>
27+
</body>
28+
</html>

examples/express-app/pages/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Home</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
text-align: center;
10+
background-color: #ffffff;
11+
color: #000000;
12+
display: flex;
13+
justify-content: center;
14+
align-items: center;
15+
height: 75vh;
16+
}
17+
h1 {
18+
font-size: 5em;
19+
margin: 0;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<div>
25+
<h1>Hello World!</h1>
26+
</div>
27+
</body>
28+
</html>

examples/express-app/server.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import express from "express";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
import fs from 'node:fs/promises';
5+
6+
import { load } from "@azure/app-configuration-provider";
7+
const connectionString = "<your-connection-string>";;
8+
const appConfig = await load(connectionString, {
9+
featureFlagOptions: {
10+
enabled: true,
11+
selectors: [{
12+
keyFilter: "*"
13+
}],
14+
refresh: {
15+
enabled: true
16+
}
17+
}
18+
});
19+
20+
appConfig.onRefresh(() => {
21+
console.log("Configuration has been refreshed.");
22+
});
23+
24+
import { ConfigurationObjectFeatureFlagProvider, ConfigurationMapFeatureFlagProvider, FeatureManager } from "@microsoft/feature-management";
25+
26+
/*
27+
You can use either ConfigurationObjectFeatureFlagProvider or ConfigurationMapFeatureFlagProvider to provide feature flags.
28+
We recommend using Azure App Configuration as the source of feature flags.
29+
*/
30+
// const config = JSON.parse(await fs.readFile("config.json"));
31+
// const featureProvider = new ConfigurationObjectFeatureFlagProvider(config);
32+
33+
const featureProvider = new ConfigurationMapFeatureFlagProvider(appConfig);
34+
const featureManager = new FeatureManager(featureProvider);
35+
36+
const app = express();
37+
const PORT = 3000;
38+
39+
const __filename = fileURLToPath(import.meta.url);
40+
const __dirname = path.dirname(__filename);
41+
42+
const pageDir = path.join(__dirname, "pages");
43+
44+
app.get("/", (req, res) => {
45+
appConfig.refresh();
46+
res.sendFile(path.join(pageDir, "index.html"));
47+
});
48+
49+
app.get("/Beta", async (req, res) => {
50+
appConfig.refresh();
51+
const { userId, groups } = req.query;
52+
53+
if (await featureManager.isEnabled("Beta", { userId: userId, groups: groups ? groups.split(",") : [] })) {
54+
res.sendFile(path.join(pageDir, "beta.html"));
55+
} else {
56+
res.status(404).send("Page not found");
57+
}
58+
});
59+
60+
// Start the server
61+
app.listen(PORT, () => {
62+
console.log(`Server is running at http://localhost:${PORT}`);
63+
});

0 commit comments

Comments
 (0)