Skip to content

Instantly share code, notes, and snippets.

@JanneMattila
Created April 24, 2019 19:20
Show Gist options
  • Save JanneMattila/dfcf9d11a7f8ca02396c29a8922b1041 to your computer and use it in GitHub Desktop.
Save JanneMattila/dfcf9d11a7f8ca02396c29a8922b1041 to your computer and use it in GitHub Desktop.
Azure App Configuration
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. */ };
}
}
}
}
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; }
}
}
// ...
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));
});
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