Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dependencies": {
"@azure/app-configuration-provider": "latest",
"@azure/identity": "^4.1.0",
"dotenv": "^16.3.1"
"dotenv": "^16.3.1",
"express": "^4.21.2"
}
}
12 changes: 5 additions & 7 deletions examples/refresh.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ const settings = await load(connectionString, {
refreshOptions: {
enabled: true,
watchedSettings: [{ key: "app.settings.sentinel" }],
refreshIntervalInMs: 10 * 1000 // Default value is 30 seconds, shorted for this sample
refreshIntervalInMs: 10_000 // Default value is 30 seconds, shorted for this sample
}
});

console.log("Using Azure portal or CLI, update the `app.settings.message` value, and then update the `app.settings.sentinel` value in your App Configuration store.")

// eslint-disable-next-line no-constant-condition
while (true) {
// Refreshing the configuration setting
await settings.refresh();

// Current value of message
console.log(settings.get("message"));

// Waiting before the next refresh
await sleepInMs(5000);
// Refreshing the configuration setting asynchronously
settings.refresh();
// wait for 5 seconds
await sleepInMs(5_000);
}
51 changes: 51 additions & 0 deletions examples/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";
dotenv.config()

import { load } from "@azure/app-configuration-provider";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = process.env.APPCONFIG_ENDPOINT;
const credential = new DefaultAzureCredential();
const appConfig = await load(endpoint, credential, {
refreshOptions: {
enabled: true,
refreshIntervalInMs: 5_000
}
});

appConfig.onRefresh(() => {
console.log("Configuration has been refreshed.");
});

import express from "express";

const server = express();
const PORT = 3000;

server.use(express.json());

// Use a middleware to achieve request-driven configuration refresh
// For more information, please go to dynamic refresh tutorial: https://learn.microsoft.com/azure/azure-app-configuration/enable-dynamic-configuration-javascript
server.use((req, res, next) => {
// this call s not blocking, the configuration will be updated asynchronously
appConfig.refresh();
next();
});

server.get("/", (req, res) => {
res.send("Please go to /config to get the configuration.");
});

server.get("/config", (req, res) => {
res.json(appConfig.constructConfigurationObject());
});

server.get("/config/:key", (req, res) => {
res.json(appConfig.get(req.params.key) ?? "");
});

server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
2 changes: 1 addition & 1 deletion src/common/disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export class Disposable {
}
this.#disposed = true;
}
}
}