Skip to content

Commit d79d9df

Browse files
Add go example apps (#1100)
* Add go example apps * update README * add empty line * update examples * updated
1 parent 143149f commit d79d9df

8 files changed

Lines changed: 534 additions & 1 deletion

File tree

examples/Go/ConsoleApp/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Azure App Configuration Console App Example
2+
3+
This example demonstrates how to use the refresh functionality of Azure App Configuration in a console/command-line application.
4+
5+
## Overview
6+
7+
This console application:
8+
9+
1. Loads configuration values from Azure App Configuration
10+
2. Binds them to target configuration struct
11+
3. Automatically refreshes the configuration when changed in Azure App Configuration
12+
13+
## Running the Example
14+
15+
### Prerequisites
16+
17+
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run the examples:
18+
19+
- [Azure App Configuration store](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal)
20+
21+
The examples retrieve credentials to access your App Configuration store from environment variables.
22+
23+
### Add key-values
24+
25+
Add the following key-values to the App Configuration store and leave **Label** and **Content Type** with their default values:
26+
27+
| Key | Value |
28+
|------------------------|----------------|
29+
| *Config.Message* | *Hello World!* |
30+
| *Config.Font.Color* | *blue* |
31+
| *Config.Font.Size* | *12* |
32+
33+
### Setup
34+
35+
1. Initialize a new Go module.
36+
37+
```bash
38+
go mod init console-example-refresh
39+
```
40+
1. Add the Azure App Configuration provider as a dependency.
41+
42+
```bash
43+
go get github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration
44+
```
45+
46+
1. Set the connection string as an environment variable:
47+
48+
```bash
49+
# Windows
50+
set AZURE_APPCONFIG_CONNECTION_STRING=your-connection-string
51+
52+
# Linux/macOS
53+
export AZURE_APPCONFIG_CONNECTION_STRING=your-connection-string
54+
```
55+
56+
### Run the Application
57+
58+
```bash
59+
go run main.go
60+
```
61+
62+
### Testing the Refresh Functionality
63+
64+
1. Start the application
65+
2. While it's running, modify the values in your Azure App Configuration store
66+
3. Within 10 seconds (the configured refresh interval), the application should detect and apply the changes
67+
4. You don't need to restart the application to see the updated values

examples/Go/ConsoleApp/main.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
13+
)
14+
15+
type Config struct {
16+
Font Font
17+
Message string
18+
}
19+
20+
type Font struct {
21+
Color string
22+
Size int
23+
}
24+
25+
func main() {
26+
// Load configuration from Azure App Configuration
27+
configProvider, err := loadAzureAppConfiguration()
28+
if err != nil {
29+
log.Fatalf("Error loading configuration: %s", err)
30+
}
31+
32+
// Parse initial configuration into struct
33+
var config Config
34+
err = configProvider.Unmarshal(&config, nil)
35+
if err != nil {
36+
log.Fatalf("Error unmarshalling configuration: %s", err)
37+
}
38+
39+
// Display the initial configuration
40+
displayConfig(config)
41+
42+
// Register refresh callback to update and display the configuration
43+
configProvider.OnRefreshSuccess(func() {
44+
fmt.Println("\n Configuration changed! Updating values...")
45+
46+
// Re-unmarshal the configuration
47+
var updatedConfig Config
48+
err := configProvider.Unmarshal(&updatedConfig, nil)
49+
if err != nil {
50+
log.Printf("Error unmarshalling updated configuration: %s", err)
51+
return
52+
}
53+
54+
// Update our working config
55+
config = updatedConfig
56+
57+
// Display the updated configuration
58+
displayConfig(config)
59+
})
60+
61+
// Setup a channel to listen for termination signals
62+
done := make(chan os.Signal, 1)
63+
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
64+
65+
fmt.Println("\nWaiting for configuration changes...")
66+
fmt.Println("(Update values in Azure App Configuration to see refresh in action)")
67+
fmt.Println("Press Ctrl+C to exit")
68+
69+
// Start a ticker to periodically trigger refresh
70+
ticker := time.NewTicker(5 * time.Second)
71+
defer ticker.Stop()
72+
73+
// Keep the application running until terminated
74+
for {
75+
select {
76+
case <-ticker.C:
77+
// Trigger refresh in background
78+
go func() {
79+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
80+
defer cancel()
81+
82+
if err := configProvider.Refresh(ctx); err != nil {
83+
log.Printf("Error refreshing configuration: %s", err)
84+
}
85+
}()
86+
case <-done:
87+
fmt.Println("\nExiting...")
88+
return
89+
}
90+
}
91+
}
92+
93+
// loadAzureAppConfiguration loads the configuration from Azure App Configuration
94+
func loadAzureAppConfiguration() (*azureappconfiguration.AzureAppConfiguration, error) {
95+
// Get connection string from environment variable
96+
connectionString := os.Getenv("AZURE_APPCONFIG_CONNECTION_STRING")
97+
98+
// Options setup
99+
options := &azureappconfiguration.Options{
100+
Selectors: []azureappconfiguration.Selector{
101+
{
102+
KeyFilter: "Config.*",
103+
},
104+
},
105+
// Remove the prefix when mapping to struct fields
106+
TrimKeyPrefixes: []string{"Config."},
107+
// Enable refresh every 10 seconds
108+
RefreshOptions: azureappconfiguration.KeyValueRefreshOptions{
109+
Enabled: true,
110+
Interval: 10 * time.Second,
111+
},
112+
}
113+
114+
authOptions := azureappconfiguration.AuthenticationOptions{
115+
ConnectionString: connectionString,
116+
}
117+
118+
// Create configuration provider with timeout
119+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
120+
defer cancel()
121+
122+
return azureappconfiguration.Load(ctx, authOptions, options)
123+
}
124+
125+
// displayConfig prints the current configuration values
126+
func displayConfig(config Config) {
127+
fmt.Println("\nCurrent Configuration Values:")
128+
fmt.Println("--------------------")
129+
fmt.Printf("Font Color: %s\n", config.Font.Color)
130+
fmt.Printf("Font Size: %d\n", config.Font.Size)
131+
fmt.Printf("Message: %s\n", config.Message)
132+
fmt.Println("--------------------")
133+
}

examples/Go/WebApp/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Gin Feature Flags Web App
2+
3+
This is a Gin web application using a feature flag in Azure App Configuration to dynamically control the availability of a new web page without restarting or redeploying it.
4+
5+
## Prerequisites
6+
7+
- An Azure account with an active subscription
8+
- An Azure App Configuration store
9+
- A feature flag named "Beta" in your App Configuration store
10+
- Go 1.23 or later
11+
12+
## Running the Example
13+
14+
1. **Create a feature flag in Azure App Configuration:**
15+
16+
Add a feature flag called *Beta* to the App Configuration store and leave **Label** and **Description** with their default values. For more information about how to add feature flags to a store using the Azure portal or the CLI, go to [Create a feature flag](https://learn.microsoft.com/azure/azure-app-configuration/manage-feature-flags?tabs=azure-portal#create-a-feature-flag).
17+
18+
2. **Set environment variable:**
19+
20+
**Windows PowerShell:**
21+
```powershell
22+
$env:AZURE_APPCONFIG_CONNECTION_STRING = "your-connection-string"
23+
```
24+
25+
**Windows Command Prompt:**
26+
```cmd
27+
setx AZURE_APPCONFIG_CONNECTION_STRING "your-connection-string"
28+
```
29+
30+
**Linux/macOS:**
31+
```bash
32+
export AZURE_APPCONFIG_CONNECTION_STRING="your-connection-string"
33+
```
34+
35+
## Running the Application
36+
37+
```bash
38+
go run main.go
39+
```
40+
41+
Open http://localhost:8080 in your browser.
42+
43+
## Testing the Feature Flag
44+
45+
1. **Start the application** - Beta menu item should be hidden (feature disabled)
46+
2. **Go to Azure portal** → App Configuration → Feature manager
47+
3. **Enable the "Beta" feature flag**
48+
4. **Wait up to 30 seconds** and refresh the page
49+
5. **Observe the Beta menu item** appears in navigation
50+
6. **Click the Beta menu** to access the Beta page
51+
7. **Disable the flag again** and test that `/beta` returns 404

0 commit comments

Comments
 (0)