Skip to content

Commit 8041858

Browse files
authored
Merge pull request #303032 from linglingye001/go/quickstart/fix
Update quickstarts
2 parents dce153c + 05cf31a commit 8041858

6 files changed

+61
-78
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-gin-web-app.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Finish the quickstart: [Create a Gin web app with Azure App Configuration](./qui
4040
> [!TIP]
4141
> You can set the `Interval` property of the `RefreshOptions` to specify the minimum time between configuration refreshes. In this example, you use the default value of 30 seconds. Adjust to a higher value if you need to reduce the number of requests made to your App Configuration store.
4242

43-
2. Update your `main.go` file to register a callback function for configuration updates:
43+
1. Update your `main.go` file to register a callback function for configuration updates:
4444

4545
```golang
4646
// Existing code
@@ -59,11 +59,21 @@ Finish the quickstart: [Create a Gin web app with Azure App Configuration](./qui
5959
return
6060
}
6161
})
62+
63+
// The rest of existing code
64+
//... ...
6265
```
6366

64-
3. Add a configuration refresh middleware. Update *`main.go`* with the following code.
67+
1. Add a configuration refresh middleware. Update *`main.go`* with the following code.
6568

6669
```golang
70+
// Existing code
71+
// ... ...
72+
type App struct {
73+
Name string
74+
Port int
75+
}
76+
6777
func configRefreshMiddleware(provider *azureappconfiguration.AzureAppConfiguration) gin.HandlerFunc {
6878
return func(c *gin.Context) {
6979
// Start refresh in a goroutine to avoid blocking the request
@@ -79,9 +89,12 @@ Finish the quickstart: [Create a Gin web app with Azure App Configuration](./qui
7989
c.Next()
8090
}
8191
}
92+
93+
// The rest of existing code
94+
//... ...
8295
```
8396

84-
4. Use the configuration refresh middleware:
97+
1. Use the configuration refresh middleware:
8598

8699
```golang
87100
// Existing code
@@ -110,20 +123,21 @@ Now that you've set up dynamic configuration refresh, let's test it to see it in
110123
1. Run the application.
111124

112125
```bash
113-
go run main.go
126+
go mod tidy
127+
go run .
114128
```
115129

116-
2. Open a web browser and navigate to `http://localhost:8080` to access your application. The web page looks like this:
130+
1. Open a web browser and navigate to `http://localhost:8080` to access your application. The web page looks like this:
117131

118132
:::image type="content" source="./media/quickstarts/gin-app-refresh-before.png" alt-text="Screenshot of the gin web app refresh before.":::
119133

120-
3. Navigate to your App Configuration store and update the value of the `Config.Message` key.
134+
1. Navigate to your App Configuration store and update the value of the `Config.Message` key.
121135

122136
| Key | Value | Content type |
123137
|------------------------|----------------------------------------|--------------------|
124138
| *Config.Message* | *Hello from Azure App Configuration - now with live updates!* | Leave empty |
125139

126-
4. After refreshing the browser a few times, you'll see the updated content once the ConfigMap is updated in 30 seconds.
140+
1. After refreshing the browser a few times, you'll see the updated content once the ConfigMap is updated in 30 seconds.
127141
128142
:::image type="content" source="./media/quickstarts/gin-app-refresh-after.png" alt-text="Screenshot of the gin web app refresh after.":::
129143

articles/azure-app-configuration/enable-dynamic-configuration-go-console-app.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,27 @@ In this quickstart, you'll enhance a basic Go console application to dynamically
4141
> [!TIP]
4242
> You can set the `Interval` property of the `RefreshOptions` to specify the minimum time between configuration refreshes. In this example, you use the default value of 30 seconds. Adjust to a higher value if you need to reduce the number of requests made to your App Configuration store.
4343

44-
2. Open the file *`unmarshal_sample.go`* and add the following code to your main function:
44+
2. Open the file *`main.go`* and add the following code to your main function:
4545

4646
```golang
47-
// Existing code in unmarshal_sample.go
47+
// Existing code in main.go
4848
// ... ...
49-
fmt.Printf("Timeout: %d seconds\n", config.App.Settings.Timeout)
50-
fmt.Printf("Retry Count: %d\n", config.App.Settings.RetryCount)
51-
52-
// Register refresh callback to update and display the configuration
53-
provider.OnRefreshSuccess(func() {
54-
// Re-unmarshal the configuration
55-
err := appCfgProvider.Unmarshal(&updatedConfig, nil)
56-
if err != nil {
57-
log.Printf("Error unmarshalling updated configuration: %s", err)
58-
return
59-
}
60-
61-
// Display the updated configuration
62-
displayConfig(config)
63-
})
49+
fmt.Println("\nRaw JSON Configuration:")
50+
fmt.Println("------------------------")
51+
fmt.Println(string(jsonBytes))
52+
53+
// Register refresh callback to update the configuration
54+
provider.OnRefreshSuccess(func() {
55+
var updatedConfig Config
56+
// Re-unmarshal the configuration
57+
err := provider.Unmarshal(&updatedConfig, nil)
58+
if err != nil {
59+
log.Printf("Error unmarshalling updated configuration: %s", err)
60+
return
61+
}
62+
63+
fmt.Printf("Message: %s\n", updatedConfig.Message)
64+
})
6465
6566
// Setup a channel to listen for termination signals
6667
done := make(chan os.Signal, 1)
@@ -99,7 +100,8 @@ In this quickstart, you'll enhance a basic Go console application to dynamically
99100
1. Run your application:
100101

101102
```bash
102-
go run unmarshal_sample.go
103+
go mod tidy
104+
go run .
103105
```
104106

105107
2. Keep the application running.

articles/azure-app-configuration/quickstart-feature-flag-go-console.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
164164
1. Run the application:
165165

166166
```console
167-
go run main.go
167+
go mod tidy
168+
go run .
168169
```
169170
---
170171

articles/azure-app-configuration/quickstart-feature-flag-go-gin.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
243243
betaEnabled := c.GetBool("betaEnabled")
244244

245245
c.HTML(http.StatusOK, "index.html", gin.H{
246-
"title": "Feature Management Demo",
246+
"title": "Feature Management Example App",
247247
"betaEnabled": betaEnabled,
248248
})
249249
}
@@ -311,7 +311,8 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
311311
1. [Set the environment variable for authentication](./quickstart-go-web-app.md#run-the-web-application) and run the application.
312312

313313
```console
314-
go run main.go
314+
go mod tidy
315+
go run .
315316
```
316317

317318
1. Open a browser window, and go to `http://localhost:8080`. Your browser should display a page similar to the image below.

articles/azure-app-configuration/quickstart-go-console-app.md

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,14 @@ package main
6868
6969
import (
7070
"context"
71-
"fmt"
7271
"log"
7372
"os"
74-
"time"
7573
7674
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
7775
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
7876
)
7977
80-
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.Azureappconfiguration, error) {
78+
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.AzureAppConfiguration, error) {
8179
// Get the endpoint from environment variable
8280
endpoint := os.Getenv("AZURE_APPCONFIG_ENDPOINT")
8381
if endpoint == "" {
@@ -124,15 +122,13 @@ package main
124122
125123
import (
126124
"context"
127-
"fmt"
128125
"log"
129126
"os"
130-
"time"
131127
132128
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
133129
)
134130
135-
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.Azureappconfiguration, error) {
131+
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.AzureAppConfiguration, error) {
136132
// Get the connection string from environment variable
137133
connectionString := os.Getenv("AZURE_APPCONFIG_CONNECTION_STRING")
138134
if connectionString == "" {
@@ -171,7 +167,7 @@ func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.Azur
171167
172168
The `Unmarshal` method provides a type-safe way to load configuration values into Go structs. This approach prevents runtime errors from mistyped configuration keys and makes your code more maintainable. `Unmarshal` is particularly valuable for complex configurations with nested structures, different data types, or when working with microservices that require clear configuration contracts across components.
173169
174-
Create a file named `unmarshal_sample.go` with the following content:
170+
Create a file named `main.go` with the following content:
175171
176172
```golang
177173
package main
@@ -183,7 +179,6 @@ import (
183179
"time"
184180
)
185181
186-
// Configuration structure that matches your key-values in App Configuration
187182
type Config struct {
188183
Message string
189184
App struct {
@@ -228,30 +223,13 @@ func main() {
228223
229224
The `GetBytes` method retrieves your configuration as raw JSON data, offering a flexible alternative to struct binding. This approach seamlessly integrates with existing JSON processing libraries like the standard `encoding/json` package or configuration frameworks such as [`viper`](https://github.com/spf13/viper). It's particularly useful when working with dynamic configurations, when you need to store configuration temporarily, or when integrating with existing systems that expect JSON input. Using GetBytes gives you direct access to your configuration in a universally compatible format while still benefiting from Azure App Configuration's centralized management capabilities.
230225
231-
Create a file named `getbytes_sample.go` with the following content:
226+
Update `main.go` with the following content:
232227
233228
```golang
234-
package main
235-
236-
import (
237-
"context"
238-
"fmt"
239-
"log"
240-
"time"
241-
242-
"github.com/spf13/viper"
243-
)
244-
245-
func main() {
246-
// Create a context with timeout
247-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
248-
defer cancel()
249-
250-
// Load configuration using the common function
251-
provider, err := loadAzureAppConfiguration(ctx)
252-
if err != nil {
253-
log.Fatalf("Error loading configuration: %v", err)
254-
}
229+
// Existing code in main.go
230+
// ... ...
231+
fmt.Printf("Timeout: %d seconds\n", config.App.Settings.Timeout)
232+
fmt.Printf("Retry Count: %d\n", config.App.Settings.RetryCount)
255233
256234
// Get configuration as JSON bytes
257235
jsonBytes, err := provider.GetBytes(nil)
@@ -274,7 +252,6 @@ func main() {
274252
275253
// Use viper to access your configuration
276254
// ...
277-
}
278255
```
279256
280257
## Run the application
@@ -331,10 +308,11 @@ func main() {
331308
332309
---
333310
334-
2. After the environment variable is properly set, run the following command to run the *Unmarshal* example:
311+
2. After the environment variable is properly set, run the following command to run the *Unmarshal* and *GetBytes* example:
335312
336313
```bash
337-
go run unmarshal_sample.go
314+
go mod tidy
315+
go run .
338316
```
339317
340318
You should see output similar to the following:
@@ -347,17 +325,7 @@ func main() {
347325
Debug Mode: true
348326
Timeout: 30 seconds
349327
Retry Count: 3
350-
```
351-
352-
3. Run the *GetBytes* example:
353-
354-
```bash
355-
go run getbytes_sample.go
356-
```
357-
358-
You should see output similar to the following:
359328
360-
```Output
361329
Raw JSON Configuration:
362330
------------------------
363331
{"App":{"Debug":true,"Name":"Go Console App","Settings":{"retryCount":3,"timeout":30}},"Message":"Hello World!"}

articles/azure-app-configuration/quickstart-go-web-app.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,14 @@ package main
119119
120120
import (
121121
"context"
122-
"fmt"
123122
"log"
124123
"os"
125-
"time"
126124
127125
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
128126
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
129127
)
130128
131-
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.Azureappconfiguration, error) {
129+
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.AzureAppConfiguration, error) {
132130
// Get the endpoint from environment variable
133131
endpoint := os.Getenv("AZURE_APPCONFIG_ENDPOINT")
134132
if endpoint == "" {
@@ -175,15 +173,13 @@ package main
175173
176174
import (
177175
"context"
178-
"fmt"
179176
"log"
180177
"os"
181-
"time"
182178
183179
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
184180
)
185181
186-
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.Azureappconfiguration, error) {
182+
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.AzureAppConfiguration, error) {
187183
// Get the connection string from environment variable
188184
connectionString := os.Getenv("AZURE_APPCONFIG_CONNECTION_STRING")
189185
if connectionString == "" {
@@ -343,7 +339,8 @@ func main() {
343339
2. Run the application.
344340
345341
```bash
346-
go run main.go
342+
go mod tidy
343+
go run .
347344
```
348345
349346
You should see output similar to this:

0 commit comments

Comments
 (0)