|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as dotenv from "dotenv"; |
| 5 | +dotenv.config() |
| 6 | + |
| 7 | +import { load } from "@azure/app-configuration-provider"; |
| 8 | +import { DefaultAzureCredential } from "@azure/identity"; |
| 9 | +const endpoint = process.env.APPCONFIG_ENDPOINT; |
| 10 | +const credential = new DefaultAzureCredential(); |
| 11 | +const appConfig = await load(endpoint, credential, { |
| 12 | + refreshOptions: { |
| 13 | + enabled: true, |
| 14 | + // By default, the refresh interval is 30 seconds. You can change it by setting refreshIntervalInMs. |
| 15 | + }, |
| 16 | + keyVaultOptions:{ |
| 17 | + credential: credential |
| 18 | + } |
| 19 | +}); |
| 20 | +let config = appConfig.constructConfigurationObject(); |
| 21 | + |
| 22 | +appConfig.onRefresh(() => { |
| 23 | + config = appConfig.constructConfigurationObject(); |
| 24 | +}); |
| 25 | + |
| 26 | +import express from "express"; |
| 27 | + |
| 28 | +const server = express(); |
| 29 | +const PORT = 3000; |
| 30 | + |
| 31 | +server.use(express.json()); |
| 32 | + |
| 33 | +// Use a middleware to achieve request-driven configuration refresh |
| 34 | +// For more information, please go to dynamic refresh tutorial: https://learn.microsoft.com/azure/azure-app-configuration/enable-dynamic-configuration-javascript |
| 35 | +server.use((req, res, next) => { |
| 36 | + // this call is not blocking, the configuration will be updated asynchronously |
| 37 | + appConfig.refresh(); |
| 38 | + next(); |
| 39 | +}); |
| 40 | + |
| 41 | +server.get("/", (req, res) => { |
| 42 | + res.send(`Message from Azure App Configuration: ${config.message}`); |
| 43 | +}); |
| 44 | + |
| 45 | +server.get("/config", (req, res) => { |
| 46 | + res.json(config); |
| 47 | +}); |
| 48 | + |
| 49 | +server.listen(PORT, () => { |
| 50 | + console.log(`Server is running on http://localhost:${PORT}`); |
| 51 | +}); |
0 commit comments