Skip to content

Commit b46c652

Browse files
committed
Add go example apps
1 parent 05dc081 commit b46c652

18 files changed

Lines changed: 1297 additions & 1 deletion

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Azure App Configuration Console Example
2+
3+
This example demonstrates how to use Azure App Configuration in a console/command-line application.
4+
5+
## Overview
6+
7+
This simple console application:
8+
9+
1. Loads configuration values from Azure App Configuration
10+
2. Binds them to target configuration struct
11+
12+
## Running the Example
13+
14+
### Prerequisites
15+
16+
You need [an Azure subscription](https://azure.microsoft.com/free/) and the following Azure resources to run the examples:
17+
18+
- [Azure App Configuration store](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-azure-app-configuration-create?tabs=azure-portal)
19+
20+
The examples retrieve credentials to access your App Configuration store from environment variables.
21+
Alternatively, edit the source code to include the appropriate credentials.
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. For more information about how to add key-values to a store using the Azure portal or the CLI, go to [Create a key-value](./quickstart-azure-app-configuration-create.md#create-a-key-value).
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-app
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+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"time"
9+
10+
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
11+
)
12+
13+
type Config struct {
14+
Font Font
15+
Message string
16+
}
17+
18+
type Font struct {
19+
Color string
20+
Size int
21+
}
22+
23+
// loadConfiguration handles loading the configuration from Azure App Configuration
24+
func loadConfiguration() (Config, error) {
25+
// Get connection string from environment variable
26+
connectionString := os.Getenv("AZURE_APPCONFIG_CONNECTION_STRING")
27+
28+
// Configuration setup
29+
options := &azureappconfiguration.Options{
30+
Selectors: []azureappconfiguration.Selector{
31+
{
32+
KeyFilter: "Config.*",
33+
},
34+
},
35+
// Remove the prefix when mapping to struct fields
36+
TrimKeyPrefixes: []string{"Config."},
37+
}
38+
39+
authOptions := azureappconfiguration.AuthenticationOptions{
40+
ConnectionString: connectionString,
41+
}
42+
43+
// Create configuration provider with timeout
44+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
45+
defer cancel()
46+
47+
appCfgProvider, err := azureappconfiguration.Load(ctx, authOptions, options)
48+
if err != nil {
49+
return Config{}, err
50+
}
51+
52+
// Parse configuration into struct
53+
var config Config
54+
err = appCfgProvider.Unmarshal(&config, nil)
55+
if err != nil {
56+
return Config{}, err
57+
}
58+
59+
return config, nil
60+
}
61+
62+
func main() {
63+
fmt.Println("Azure App Configuration - Console Example")
64+
fmt.Println("----------------------------------------")
65+
66+
// Load configuration
67+
fmt.Println("Loading configuration from Azure App Configuration...")
68+
config, err := loadConfiguration()
69+
if err != nil {
70+
log.Fatalf("Error loading configuration: %s", err)
71+
}
72+
73+
// Display the configuration values
74+
fmt.Println("\nConfiguration Values:")
75+
fmt.Println("--------------------")
76+
fmt.Printf("Font Color: %s\n", config.Font.Color)
77+
fmt.Printf("Font Size: %d\n", config.Font.Size)
78+
fmt.Printf("Message: %s\n", config.Message)
79+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Azure App Configuration Console Refresh 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
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
// initializeAppConfiguration handles loading the configuration from Azure App Configuration
26+
func initializeAppConfiguration() (*azureappconfiguration.AzureAppConfiguration, error) {
27+
// Get connection string from environment variable
28+
connectionString := os.Getenv("AZURE_APPCONFIG_CONNECTION_STRING")
29+
30+
// Options setup
31+
options := &azureappconfiguration.Options{
32+
Selectors: []azureappconfiguration.Selector{
33+
{
34+
KeyFilter: "Config.*",
35+
},
36+
},
37+
// Remove the prefix when mapping to struct fields
38+
TrimKeyPrefixes: []string{"Config."},
39+
// Enable refresh every 10 seconds
40+
RefreshOptions: azureappconfiguration.KeyValueRefreshOptions{
41+
Enabled: true,
42+
Interval: 10 * time.Second,
43+
},
44+
}
45+
46+
authOptions := azureappconfiguration.AuthenticationOptions{
47+
ConnectionString: connectionString,
48+
}
49+
50+
// Create configuration provider with timeout
51+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
52+
defer cancel()
53+
54+
return azureappconfiguration.Load(ctx, authOptions, options)
55+
}
56+
57+
// displayConfig prints the current configuration values
58+
func displayConfig(config Config) {
59+
fmt.Println("\nCurrent Configuration Values:")
60+
fmt.Println("--------------------")
61+
fmt.Printf("Font Color: %s\n", config.Font.Color)
62+
fmt.Printf("Font Size: %d\n", config.Font.Size)
63+
fmt.Printf("Message: %s\n", config.Message)
64+
fmt.Println("--------------------")
65+
}
66+
67+
func main() {
68+
fmt.Println("Azure App Configuration - Console Refresh Example")
69+
fmt.Println("----------------------------------------")
70+
71+
// Load configuration
72+
fmt.Println("Loading configuration from Azure App Configuration...")
73+
appCfgProvider, err := initializeAppConfiguration()
74+
if err != nil {
75+
log.Fatalf("Error loading configuration: %s", err)
76+
}
77+
78+
// Parse initial configuration into struct
79+
var config Config
80+
err = appCfgProvider.Unmarshal(&config, nil)
81+
if err != nil {
82+
log.Fatalf("Error unmarshalling configuration: %s", err)
83+
}
84+
85+
// Display the initial configuration
86+
displayConfig(config)
87+
88+
// Register refresh callback to update and display the configuration
89+
appCfgProvider.OnRefreshSuccess(func() {
90+
fmt.Println("\n🔄 Configuration changed! Updating values...")
91+
92+
// Re-unmarshal the configuration
93+
var updatedConfig Config
94+
err := appCfgProvider.Unmarshal(&updatedConfig, nil)
95+
if err != nil {
96+
log.Printf("Error unmarshalling updated configuration: %s", err)
97+
return
98+
}
99+
100+
// Update our working config
101+
config = updatedConfig
102+
103+
// Display the updated configuration
104+
displayConfig(config)
105+
})
106+
107+
// Setup a channel to listen for termination signals
108+
done := make(chan os.Signal, 1)
109+
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
110+
111+
fmt.Println("\nWaiting for configuration changes...")
112+
fmt.Println("(Update values in Azure App Configuration to see refresh in action)")
113+
fmt.Println("Press Ctrl+C to exit")
114+
115+
// Start a ticker to periodically trigger refresh
116+
ticker := time.NewTicker(5 * time.Second)
117+
defer ticker.Stop()
118+
119+
// Keep the application running until terminated
120+
for {
121+
select {
122+
case <-ticker.C:
123+
// Trigger refresh in background
124+
go func() {
125+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
126+
defer cancel()
127+
128+
if err := appCfgProvider.Refresh(ctx); err != nil {
129+
log.Printf("Error refreshing configuration: %s", err)
130+
}
131+
}()
132+
case <-done:
133+
fmt.Println("\nExiting...")
134+
return
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)