Skip to content

Commit

Permalink
Add Feature flags (#190)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
TheTedder authored Sep 12, 2023
1 parent 93804e8 commit b7bd122
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions LeaderboardBackend/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LeaderboardBackend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement.Mvc;
using OneOf;

namespace LeaderboardBackend.Controllers;
Expand Down Expand Up @@ -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<ActionResult<UserViewModel>> Register([FromBody] RegisterRequest request)
{
CreateUserResult result = await _userService.CreateUser(request);
Expand Down Expand Up @@ -105,6 +107,7 @@ public async Task<ActionResult<UserViewModel>> Register([FromBody] RegisterReque
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[FeatureGate(Features.LOGIN)]
public async Task<ActionResult<LoginResponse>> Login([FromBody] LoginRequest request)
{
LoginResult result = await _userService.LoginByEmailAndPassword(request.Email, request.Password);
Expand Down
7 changes: 7 additions & 0 deletions LeaderboardBackend/Features.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace LeaderboardBackend;

public static class Features
{
public const string ACCOUNT_REGISTRATION = "AccountRegistration";
public const string LOGIN = "Login";
}
1 change: 1 addition & 0 deletions LeaderboardBackend/LeaderboardBackend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="2.6.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
Expand Down
4 changes: 4 additions & 0 deletions LeaderboardBackend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -280,6 +281,9 @@
// Can't use AddSingleton here since we call the DB in the Handler
builder.Services.AddScoped<IAuthorizationHandler, UserTypeAuthorizationHandler>();
builder.Services.AddSingleton<IAuthorizationMiddlewareResultHandler, MiddlewareResultHandler>();

// Enable feature management.
builder.Services.AddFeatureManagement(builder.Configuration.GetSection("Feature"));
#endregion

#region WebApplication
Expand Down
6 changes: 5 additions & 1 deletion LeaderboardBackend/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"EmailSender": {
"SenderName": "Leaderboards.gg",
"SenderAddress": "[email protected]"
}
},
"Feature": {
"AccountRegistration": true,
"Login": true
}
}
1 change: 0 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ EmailSender__Smtp__Username=User42
EmailSender__Smtp__Password=Password123

ADMINER_PORT=1337

0 comments on commit b7bd122

Please sign in to comment.