Edit

Share via


Enable features on a schedule in a Go Gin web application

In this guide, you use the time window filter to enable a feature on a schedule for a Go Gin web application.

The example used in this article is based on the Go Gin web application introduced in the feature management quickstart. Before proceeding further, complete the quickstart to create a Go Gin web application with a Beta feature flag. Once completed, you must add a time window filter to the Beta feature flag in your App Configuration store.

Prerequisites

Use the time window filter

You added a time window filter for your Beta feature flag in the prerequisites. Next, you'll use the feature flag with the time window filter in your Go Gin web application.

When you create a feature manager, the built-in feature filters are automatically added to its feature filter collection

The existing code from the quickstart already handles time window filters through the feature manager:

// Create feature flag provider
featureFlagProvider, err := azappconfig.NewFeatureFlagProvider(appConfig)
if err != nil {
    log.Fatalf("Error creating feature flag provider: %v", err)
}

// Create feature manager (supports built-in filters including TimeWindowFilter)
featureManager, err := featuremanagement.NewFeatureManager(featureFlagProvider, nil)
if err != nil {
    log.Fatalf("Error creating feature manager: %v", err)
}

The feature evaluation in your middleware will now respect the time window filter:

func (app *WebApp) featureMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        // Check if Beta feature is enabled (TimeWindowFilter is automatically evaluated)
        betaEnabled, err := app.featureManager.IsEnabled("Beta")
        if err != nil {
            log.Printf("Error checking Beta feature: %v", err)
        }

        // Store feature flag status for use in templates
        c.Set("betaEnabled", betaEnabled)
        c.Next()
    }
}

Time window filter in action

Relaunch the application. If your current time is earlier than the start time set for the time window filter, the Beta menu item won't appear on the toolbar. This is because the Beta feature flag is disabled by the time window filter.

Screenshot of Gin web app with Beta menu hidden.

Once the start time has passed, refresh your browser a few times. You'll notice that the Beta menu item now appears. This is because the Beta feature flag is now enabled by the time window filter.

Screenshot of Gin web app with Beta menu.

Next steps

To learn more about the feature filters, continue to the following documents.

For the full feature rundown of the Go feature management library, continue to the following document.