Skip to content

Commit

Permalink
feat: register services only if feature flags are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ktos committed Jun 17, 2024
1 parent 05b8e9c commit c235410
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 3 additions & 5 deletions Server/Modules/Items/Services/ItemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ public class ItemGenerator : IItemGenerator
private readonly List<RarityModifier> _modifiers;
private readonly INameGenerator _nameGenerator;
private readonly PrefixesGenerator _prefixes;
private readonly IAIInstructionProvider _ai;
private readonly IAIInstructionProvider? _ai;
private readonly IFeatureManager _featureManager;

Check warning on line 17 in Server/Modules/Items/Services/ItemGenerator.cs

View workflow job for this annotation

GitHub Actions / Checks

The field 'ItemGenerator._featureManager' is never used

public ItemGenerator(

Check warning on line 19 in Server/Modules/Items/Services/ItemGenerator.cs

View workflow job for this annotation

GitHub Actions / Checks

Non-nullable field '_featureManager' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
INameGenerator nameGenerator,
PrefixesGenerator prefixes,
IAIInstructionProvider ai,
IFeatureManager featureManager
IAIInstructionProvider? ai = null
)
{
_nameGenerator = nameGenerator;
_prefixes = prefixes;
_ai = ai;
_featureManager = featureManager;

_rnd = new Random();

Expand Down Expand Up @@ -104,7 +102,7 @@ public async Task<Item> Generate()

await _prefixes.AddPrefixes(item);

if (await _featureManager.IsEnabledAsync(FeatureFlags.USE_AI))
if (_ai is not null)
{
item.History = await GenerateDescription(item);
}
Expand Down
29 changes: 29 additions & 0 deletions Server/Modules/Shared/BuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Fracture.Server.Modules.AI.Services;
using Fracture.Server.Modules.Shared.Configuration;
using Microsoft.FeatureManagement;

namespace Fracture.Server.Modules.Shared
{
public static class BuilderExtensions
{
/// <summary>
/// Adds to services elements only if there is a proper feature flag enabled in the configuration
/// </summary>
public static async Task AddFeatureGatedServices(this IServiceCollection services)
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var featureDefinitionProvider = new ConfigurationFeatureDefinitionProvider(
configuration.GetSection(FeatureFlags.CONFIG_SECTION)
);
var featureManager = new FeatureManager(featureDefinitionProvider);

if (await featureManager.IsEnabledAsync(FeatureFlags.USE_AI))
services.AddSingleton<
IAIInstructionProvider,
OpenAICompatibleInstructionProvider
>();
}
}
}
4 changes: 3 additions & 1 deletion Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Fracture.Server.Modules.Shared.Configuration;
using Fracture.Server.Modules.Shared.NameGenerators;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.FeatureManagement;

Expand All @@ -21,7 +22,6 @@
builder.Services.AddSingleton<IItemGenerator, ItemGenerator>();
builder.Services.AddSingleton<PrefixesGenerator>();
builder.Services.AddSingleton<VersionInfoProvider>();
builder.Services.AddSingleton<IAIInstructionProvider, OpenAICompatibleInstructionProvider>();

builder.Services.AddScoped<IUsersRepository, UsersRepository>();
builder.Services.AddScoped<IItemsRepository, ItemsRepository>();
Expand All @@ -30,6 +30,8 @@
builder.Configuration.GetSection(FeatureFlags.CONFIG_SECTION)
);

await builder.Services.AddFeatureGatedServices();

builder.Services
.AddRazorComponents()
.AddInteractiveServerComponents()
Expand Down

0 comments on commit c235410

Please sign in to comment.