|
| 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 | +} |
0 commit comments