Skip to content

Commit

Permalink
Introduce Feature Flag to control MobilePay Webhooks registration (#268)
Browse files Browse the repository at this point in the history
When migrating to the new MobilePay Vipps API, the existing MobilePay
Webhooks API will be disabled, thus we need a way to disable the feature
until the new integration has been developed. We use Feature Flags to
disable registration to the Webhooks API.
  • Loading branch information
jonasanker authored Mar 11, 2024
1 parent 49ecff7 commit 1e91de1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions coffeecard/CoffeeCard.Library/CoffeeCard.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="MailKit" Version="3.4.2" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="3.2.0" />
<PackageReference Include="MimeKit" Version="3.4.2" />
<PackageReference Include="NetEscapades.Configuration.Validation" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
Expand Down
11 changes: 1 addition & 10 deletions coffeecard/CoffeeCard.Library/Services/v2/WebhookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using CoffeeCard.Models.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.FeatureManagement;
using Serilog;

namespace CoffeeCard.Library.Services.v2
Expand Down Expand Up @@ -86,16 +87,6 @@ private async Task SyncWebhook(WebhookConfiguration webhook)
await DisableAndRegisterNewWebhook(webhook);
return;
}

mobilePayWebhook = await _mobilePayWebhooksService.UpdateWebhook(mobilePayWebhook.WebhookId,
_mobilePaySettings.WebhookUrl, DefaultEvents);

webhook.Url = mobilePayWebhook.Url;
webhook.SignatureKey = mobilePayWebhook.SignatureKey;
webhook.Status = WebhookStatus.Active;
webhook.LastUpdated = DateTime.UtcNow;

await _context.SaveChangesAsync();
}
catch (EntityNotFoundException)
{
Expand Down
10 changes: 10 additions & 0 deletions coffeecard/CoffeeCard.Library/Utils/FeatureFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CoffeeCard.Library.Utils
{
public class FeatureFlags
{
/// <summary>
/// Controls whether the API should manage the registration to the MobilePayWebhooks API at startup. Disabling this feature flag, assumes that the Webhook Registration is handled outside of the Analog Core API.
/// </summary>
public const string MobilePayManageWebhookRegistration = "MobilePayManageWebhookRegistration";
}
}
3 changes: 1 addition & 2 deletions coffeecard/CoffeeCard.WebApi/CoffeeCard.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
<Compile Remove="Migrations\20200229143937_ProductUserGroups.cs" />
<Compile Remove="Migrations\20200229143937_ProductUserGroups.Designer.cs" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="7.0.0" />
<PackageReference Include="jose-jwt" Version="4.0.1" />
Expand All @@ -31,6 +29,7 @@
<PackageReference Include="Microsoft.AspNetCore.ApplicationInsights.HostingStartup" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="3.2.0" />
<PackageReference Include="NetEscapades.Configuration.Validation" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
Expand Down
10 changes: 8 additions & 2 deletions coffeecard/CoffeeCard.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using CoffeeCard.Common.Configuration;
using CoffeeCard.Library.Persistence;
using CoffeeCard.Library.Services.v2;
using CoffeeCard.Library.Utils;
using CoffeeCard.WebApi.Logging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.FeatureManagement;
using Serilog;

namespace CoffeeCard.WebApi
Expand Down Expand Up @@ -55,7 +57,7 @@ private static IHostBuilder CreateHostBuilder(string[] args)
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(((context, builder) =>
{
builder.AddJsonFile("appsettings.json", false, true);
builder.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true);
builder.AddEnvironmentVariables();
}))
.ConfigureWebHostDefaults(webBuilder =>
Expand All @@ -69,6 +71,7 @@ private static async Task PreStartupTasks(IHost webhost)
{
using var serviceScope = webhost.Services.CreateScope();
var environment = serviceScope.ServiceProvider.GetRequiredService<EnvironmentSettings>();
var featureManager = serviceScope.ServiceProvider.GetRequiredService<IFeatureManager>();

Log.Information("Apply Database Migrations if any");
await using var context = serviceScope.ServiceProvider.GetRequiredService<CoffeeCardContext>();
Expand All @@ -77,7 +80,10 @@ private static async Task PreStartupTasks(IHost webhost)
context.Database.Migrate();
}

if (environment.EnvironmentType != EnvironmentType.LocalDevelopment)
var isMobilePayWebhookRegistrationManagementEnabled = await featureManager.IsEnabledAsync(FeatureFlags.MobilePayManageWebhookRegistration);
Log.Information("FeatureFlag {flag} has enablement state '{value}'", nameof(FeatureFlags.MobilePayManageWebhookRegistration), isMobilePayWebhookRegistrationManagementEnabled);

if (environment.EnvironmentType != EnvironmentType.LocalDevelopment && isMobilePayWebhookRegistrationManagementEnabled)
{
var webhookService = serviceScope.ServiceProvider.GetRequiredService<IWebhookService>();
await webhookService.EnsureWebhookIsRegistered();
Expand Down
2 changes: 2 additions & 0 deletions coffeecard/CoffeeCard.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.FeatureManagement;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
Expand Down Expand Up @@ -94,6 +95,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<Library.Services.v2.ILeaderboardService, Library.Services.v2.LeaderboardService>();
services.AddScoped<IStatisticService, StatisticService>();
services.AddScoped<IDateTimeProvider, DateTimeProvider>();
services.AddFeatureManagement();

// Azure Application Insights
services.AddApplicationInsightsTelemetry();
Expand Down
3 changes: 3 additions & 0 deletions coffeecard/CoffeeCard.WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@
}
}
]
},
"FeatureManagement": {
"MobilePayManageWebhookRegistration": false
}
}

0 comments on commit 1e91de1

Please sign in to comment.