-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathmain.go
More file actions
157 lines (129 loc) · 3.96 KB
/
Copy pathmain.go
File metadata and controls
157 lines (129 loc) · 3.96 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
"github.com/gin-gonic/gin"
"github.com/microsoft/Featuremanagement-Go/featuremanagement"
"github.com/microsoft/Featuremanagement-Go/featuremanagement/providers/azappconfig"
)
type WebApp struct {
featureManager *featuremanagement.FeatureManager
appConfig *azureappconfiguration.AzureAppConfiguration
}
func main() {
ctx := context.Background()
// Load config from Azure App Configuration
appConfig, err := loadAzureAppConfiguration(ctx)
if err != nil {
log.Fatalf("Error loading Azure App Configuration: %v", err)
}
// Create feature flag provider
featureFlagProvider, err := azappconfig.NewFeatureFlagProvider(appConfig)
if err != nil {
log.Fatalf("Error creating feature flag provider: %v", err)
}
// Create feature manager
featureManager, err := featuremanagement.NewFeatureManager(featureFlagProvider, nil)
if err != nil {
log.Fatalf("Error creating feature manager: %v", err)
}
// Create web app
app := &WebApp{
featureManager: featureManager,
appConfig: appConfig,
}
// Setup Gin with default middleware (Logger and Recovery)
r := gin.Default()
// Setup routes
app.setupRoutes(r)
// Start server
fmt.Println("Starting server on http://localhost:8080")
fmt.Println("Open http://localhost:8080 in your browser")
fmt.Println("Toggle the 'Beta' feature flag in Azure portal to see changes")
fmt.Println()
if err := r.Run(":8080"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.AzureAppConfiguration, error) {
connectionString := os.Getenv("AZURE_APPCONFIG_CONNECTION_STRING")
if connectionString == "" {
return nil, fmt.Errorf("AZURE_APPCONFIG_CONNECTION_STRING environment variable is not set")
}
authOptions := azureappconfiguration.AuthenticationOptions{
ConnectionString: connectionString,
}
options := &azureappconfiguration.Options{
FeatureFlagOptions: azureappconfiguration.FeatureFlagOptions{
Enabled: true,
Selectors: []azureappconfiguration.Selector{
{
KeyFilter: "*",
LabelFilter: "",
},
},
RefreshOptions: azureappconfiguration.RefreshOptions{
Enabled: true,
},
},
}
appConfig, err := azureappconfiguration.Load(ctx, authOptions, options)
if err != nil {
return nil, fmt.Errorf("failed to load configuration: %w", err)
}
return appConfig, nil
}
func (app *WebApp) featureMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// Refresh configuration to get latest feature flags
ctx := context.Background()
if err := app.appConfig.Refresh(ctx); err != nil {
log.Printf("Error refreshing configuration: %v", err)
}
// Check if Beta feature is enabled
betaEnabled, err := app.featureManager.IsEnabled("Beta")
if err != nil {
log.Printf("Error checking Beta feature: %v", err)
betaEnabled = false
}
// Store feature flag status for use in templates
c.Set("betaEnabled", betaEnabled)
c.Next()
}
}
func (app *WebApp) setupRoutes(r *gin.Engine) {
// Apply feature middleware to all routes
r.Use(app.featureMiddleware())
// Load HTML templates
r.LoadHTMLGlob("templates/*.html")
// Routes
r.GET("/", app.homeHandler)
r.GET("/beta", app.betaHandler)
}
// Home page handler
func (app *WebApp) homeHandler(c *gin.Context) {
betaEnabled := c.GetBool("betaEnabled")
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Feature Management Demo",
"betaEnabled": betaEnabled,
})
}
// Beta page handler
func (app *WebApp) betaHandler(c *gin.Context) {
betaEnabled := c.GetBool("betaEnabled")
// Feature gate logic - return 404 if feature is not enabled
if !betaEnabled {
c.HTML(http.StatusNotFound, "404.html", gin.H{
"title": "Page Not Found",
"message": "The page you are looking for does not exist or is not available.",
})
return
}
c.HTML(http.StatusOK, "beta.html", gin.H{
"title": "Beta Page",
})
}