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