Created
April 24, 2019 19:20
-
-
Save JanneMattila/dfcf9d11a7f8ca02396c29a8922b1041 to your computer and use it in GitHub Desktop.
Azure App Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using FeatureTogglerApp.Features; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Options; | |
namespace FeatureTogglerApp.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class DataController : ControllerBase | |
{ | |
private readonly FeatureOptions _featureOptions; | |
public DataController(IOptionsSnapshot<FeatureOptions> featureOptions) | |
{ | |
_featureOptions = featureOptions.Value; | |
} | |
[HttpGet] | |
public IEnumerable<string> Get() | |
{ | |
var user = "janne"; // DEMO: Example user to look for feature flags | |
// Use app level toggles first and then override with | |
// possible (but not mandatory) user toggles | |
var dataApiEnabled = | |
_featureOptions.DataApiEnabled || | |
(_featureOptions.Users.ContainsKey(user) && | |
_featureOptions.Users[user].DataApiEnabled); | |
if (dataApiEnabled) | |
{ | |
return new string[] { "cool", "api", "in", "use" }; | |
} | |
else | |
{ | |
return new string[] { /* None since api not in use. */ }; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace FeatureTogglerApp.Features | |
{ | |
public class FeatureOptions : FeatureToggles | |
{ | |
public string AppName { get; set; } | |
public Dictionary<string, FeatureToggles> Users { get; set; } | |
public FeatureOptions() | |
{ | |
Users = new Dictionary<string, FeatureToggles>(); | |
} | |
} | |
public class FeatureToggles | |
{ | |
public bool IndexPageCanvasEnabled { get; set; } | |
public bool DataApiEnabled { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
config.AddAzureAppConfiguration(options => | |
{ | |
// Use with connection string: | |
// options.Connect(connectionString) | |
// or with managed identity: | |
options.ConnectWithManagedIdentity(endpoint) | |
.Use("FeatureToggler:*") | |
.WatchAndReloadAll("FeatureToggler:UpdateKey", TimeSpan.FromSeconds(5)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.Configure<FeatureOptions>(Configuration.GetSection("FeatureToggler")); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment