Skip to content

Add server example #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add server example
  • Loading branch information
zhiyuanliang-ms committed Jan 16, 2025
commit 7db4c1d41cc2e19bac9a60af31973d70e4f6a0cf
31 changes: 31 additions & 0 deletions examples/express-app/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"feature_management": {
"feature_flags": [
{
"id": "Beta",
"enabled": true,
"conditions": {
"client_filters": [
{
"name": "Microsoft.Targeting",
"parameters": {
"Audience": {
"Users": [
"Jeff"
],
"Groups": [
{
"Name": "Admin",
"RolloutPercentage": 100
}
],
"DefaultRolloutPercentage": 40
}
}
}
]
}
}
]
}
}
7 changes: 7 additions & 0 deletions examples/express-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"@azure/app-configuration-provider": "latest",
"@microsoft/feature-management": "latest",
"express": "^4.21.2"
}
}
28 changes: 28 additions & 0 deletions examples/express-app/pages/beta.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Beta</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #ffffff;
color: #000000;
display: flex;
justify-content: center;
align-items: center;
height: 75vh;
}
h1 {
font-size: 5em;
margin: 0;
}
</style>
</head>
<body>
<div>
<h1>Welcome to the Beta page!</h1>
</div>
</body>
</html>
28 changes: 28 additions & 0 deletions examples/express-app/pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #ffffff;
color: #000000;
display: flex;
justify-content: center;
align-items: center;
height: 75vh;
}
h1 {
font-size: 5em;
margin: 0;
}
</style>
</head>
<body>
<div>
<h1>Hello World!</h1>
</div>
</body>
</html>
63 changes: 63 additions & 0 deletions examples/express-app/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import fs from 'node:fs/promises';

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.
*/
// 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 app = express();
const PORT = 3000;

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const pageDir = path.join(__dirname, "pages");

app.get("/", (req, res) => {
appConfig.refresh();
res.sendFile(path.join(pageDir, "index.html"));
});

app.get("/Beta", async (req, res) => {
appConfig.refresh();
const { userId, groups } = req.query;

if (await featureManager.isEnabled("Beta", { userId: userId, groups: groups ? groups.split(",") : [] })) {
res.sendFile(path.join(pageDir, "beta.html"));
} else {
res.status(404).send("Page not found");
}
});

// Start the server
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
Loading