1+ package main
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "log"
7+ "net/http"
8+
9+ "github.com/Azure/AppConfiguration-GoProvider/azureappconfiguration"
10+ "github.com/gin-gonic/gin"
11+ "github.com/microsoft/Featuremanagement-Go/featuremanagement"
12+ "github.com/microsoft/Featuremanagement-Go/featuremanagement/providers/azappconfig"
13+ )
14+
15+ type WebApp struct {
16+ featureManager * featuremanagement.FeatureManager
17+ appConfig * azureappconfiguration.AzureAppConfiguration
18+ }
19+
20+ func (app * WebApp ) refreshMiddleware () gin.HandlerFunc {
21+ return func (c * gin.Context ) {
22+ go func () {
23+ ctx := context .Background ()
24+ if err := app .appConfig .Refresh (ctx ); err != nil {
25+ log .Printf ("Error refreshing configuration: %v" , err )
26+ }
27+ }()
28+ c .Next ()
29+ }
30+ }
31+
32+ func (app * WebApp ) featureMiddleware () gin.HandlerFunc {
33+ return func (c * gin.Context ) {
34+ // Check if Beta feature is enabled
35+ betaEnabled , err := app .featureManager .IsEnabled ("Beta" )
36+ if err != nil {
37+ log .Printf ("Error checking Beta feature: %v" , err )
38+ betaEnabled = false
39+ }
40+
41+ // Store feature flag status for use in templates
42+ c .Set ("betaEnabled" , betaEnabled )
43+ c .Next ()
44+ }
45+ }
46+
47+ func (app * WebApp ) setupRoutes (r * gin.Engine ) {
48+ r .Use (app .refreshMiddleware ())
49+ r .Use (app .featureMiddleware ())
50+
51+ // Load HTML templates
52+ r .LoadHTMLGlob ("templates/*.html" )
53+
54+ // Routes
55+ r .GET ("/" , app .homeHandler )
56+ r .GET ("/beta" , app .betaHandler )
57+ }
58+
59+ // Home page handler
60+ func (app * WebApp ) homeHandler (c * gin.Context ) {
61+ betaEnabled := c .GetBool ("betaEnabled" )
62+
63+ c .HTML (http .StatusOK , "index.html" , gin.H {
64+ "title" : "Feature Management Example App" ,
65+ "betaEnabled" : betaEnabled ,
66+ })
67+ }
68+
69+ // Beta page handler
70+ func (app * WebApp ) betaHandler (c * gin.Context ) {
71+ betaEnabled := c .GetBool ("betaEnabled" )
72+
73+ if ! betaEnabled {
74+ return
75+ }
76+
77+ c .HTML (http .StatusOK , "beta.html" , gin.H {
78+ "title" : "Beta Page" ,
79+ })
80+ }
81+
82+ func main () {
83+ ctx := context .Background ()
84+
85+ // Load Azure App Configuration
86+ appConfig , err := loadAzureAppConfiguration (ctx )
87+ if err != nil {
88+ log .Fatalf ("Error loading Azure App Configuration: %v" , err )
89+ }
90+
91+ // Create feature flag provider
92+ featureFlagProvider , err := azappconfig .NewFeatureFlagProvider (appConfig )
93+ if err != nil {
94+ log .Fatalf ("Error creating feature flag provider: %v" , err )
95+ }
96+
97+ // Create feature manager
98+ featureManager , err := featuremanagement .NewFeatureManager (featureFlagProvider , nil )
99+ if err != nil {
100+ log .Fatalf ("Error creating feature manager: %v" , err )
101+ }
102+
103+ // Create web app
104+ app := & WebApp {
105+ featureManager : featureManager ,
106+ appConfig : appConfig ,
107+ }
108+
109+ // Set up Gin with default middleware (Logger and Recovery)
110+ r := gin .Default ()
111+
112+ // Set up routes
113+ app .setupRoutes (r )
114+
115+ // Start server
116+ fmt .Println ("Starting server on http://localhost:8080" )
117+ fmt .Println ("Open http://localhost:8080 in your browser" )
118+ fmt .Println ("Toggle the 'Beta' feature flag in Azure portal to see changes" )
119+ fmt .Println ()
120+
121+ if err := r .Run (":8080" ); err != nil {
122+ log .Fatalf ("Failed to start server: %v" , err )
123+ }
124+ }
0 commit comments