From b7bd122df0089623b07481f2e66534281bc5ea02 Mon Sep 17 00:00:00 2001 From: Ted Wollman <25165500+TheTedder@users.noreply.github.com> Date: Tue, 12 Sep 2023 00:52:23 -0400 Subject: [PATCH] Add Feature flags (#190) * Add feature management package. * Enable feature management. * Create Features class. * Add feature gates to login and registration. * Enable all features in example.env. * Update the test workflow with feature env vars. * dotnet format * Remove feature env vars from test workflow. * Enable features by default in appsettings.json. * Remove feature env vars from example.env. * Move Features.cs. --- LeaderboardBackend/Controllers/AccountController.cs | 3 +++ LeaderboardBackend/Features.cs | 7 +++++++ LeaderboardBackend/LeaderboardBackend.csproj | 1 + LeaderboardBackend/Program.cs | 4 ++++ LeaderboardBackend/appsettings.json | 6 +++++- example.env | 1 - 6 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 LeaderboardBackend/Features.cs diff --git a/LeaderboardBackend/Controllers/AccountController.cs b/LeaderboardBackend/Controllers/AccountController.cs index 6af8030f..432cf520 100644 --- a/LeaderboardBackend/Controllers/AccountController.cs +++ b/LeaderboardBackend/Controllers/AccountController.cs @@ -5,6 +5,7 @@ using LeaderboardBackend.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.FeatureManagement.Mvc; using OneOf; namespace LeaderboardBackend.Controllers; @@ -53,6 +54,7 @@ public AccountController(IUserService userService) [ApiConventionMethod(typeof(Conventions), nameof(Conventions.PostAnon))] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status409Conflict, Type = typeof(ValidationProblemDetails))] + [FeatureGate(Features.ACCOUNT_REGISTRATION)] public async Task> Register([FromBody] RegisterRequest request) { CreateUserResult result = await _userService.CreateUser(request); @@ -105,6 +107,7 @@ public async Task> Register([FromBody] RegisterReque [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [FeatureGate(Features.LOGIN)] public async Task> Login([FromBody] LoginRequest request) { LoginResult result = await _userService.LoginByEmailAndPassword(request.Email, request.Password); diff --git a/LeaderboardBackend/Features.cs b/LeaderboardBackend/Features.cs new file mode 100644 index 00000000..348aec4f --- /dev/null +++ b/LeaderboardBackend/Features.cs @@ -0,0 +1,7 @@ +namespace LeaderboardBackend; + +public static class Features +{ + public const string ACCOUNT_REGISTRATION = "AccountRegistration"; + public const string LOGIN = "Login"; +} diff --git a/LeaderboardBackend/LeaderboardBackend.csproj b/LeaderboardBackend/LeaderboardBackend.csproj index 51515a5a..c9f65b4b 100644 --- a/LeaderboardBackend/LeaderboardBackend.csproj +++ b/LeaderboardBackend/LeaderboardBackend.csproj @@ -35,6 +35,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + diff --git a/LeaderboardBackend/Program.cs b/LeaderboardBackend/Program.cs index 0c5265c7..ffe06887 100644 --- a/LeaderboardBackend/Program.cs +++ b/LeaderboardBackend/Program.cs @@ -20,6 +20,7 @@ using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; +using Microsoft.FeatureManagement; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using NodaTime; @@ -280,6 +281,9 @@ // Can't use AddSingleton here since we call the DB in the Handler builder.Services.AddScoped(); builder.Services.AddSingleton(); + +// Enable feature management. +builder.Services.AddFeatureManagement(builder.Configuration.GetSection("Feature")); #endregion #region WebApplication diff --git a/LeaderboardBackend/appsettings.json b/LeaderboardBackend/appsettings.json index b8aacfa9..0e26077b 100644 --- a/LeaderboardBackend/appsettings.json +++ b/LeaderboardBackend/appsettings.json @@ -10,5 +10,9 @@ "EmailSender": { "SenderName": "Leaderboards.gg", "SenderAddress": "no-reply@leaderboards.gg" - } + }, + "Feature": { + "AccountRegistration": true, + "Login": true + } } diff --git a/example.env b/example.env index 0d9ac5ea..65c9600d 100644 --- a/example.env +++ b/example.env @@ -20,4 +20,3 @@ EmailSender__Smtp__Username=User42 EmailSender__Smtp__Password=Password123 ADMINER_PORT=1337 -