81 lines
2.1 KiB
C#
81 lines
2.1 KiB
C#
using ModpackUpdater.Model;
|
|
using Unleash;
|
|
|
|
namespace ModpackUpdater;
|
|
|
|
public enum AppFeatures
|
|
{
|
|
AllowExtras
|
|
}
|
|
|
|
public static class AppFeaturesExtensions
|
|
{
|
|
private const string apiUrl = "https://git.pilzinsel64.de/api/v4/feature_flags/unleash/2";
|
|
private const string instanceId = "glffct-3vCzJXChAnxjsgvoHijR";
|
|
|
|
private static IUnleash api;
|
|
private static UnleashContext context;
|
|
private static UnleashSettings settings;
|
|
|
|
public static bool IsEnabled(this AppFeatures feature, AppFeatureContext context)
|
|
{
|
|
return feature switch
|
|
{
|
|
AppFeatures.AllowExtras => CheckFeature("allow-extras", false, context),
|
|
_ => throw new NotSupportedException(),
|
|
};
|
|
}
|
|
|
|
public static bool IsEnabled(this AppFeatures feature)
|
|
{
|
|
return feature switch
|
|
{
|
|
_ => throw new NotSupportedException(),
|
|
};
|
|
}
|
|
|
|
private static bool InitializeApi()
|
|
{
|
|
if (api == null)
|
|
{
|
|
settings = new UnleashSettings
|
|
{
|
|
AppName = "Modpack Updater",
|
|
UnleashApi = new Uri(apiUrl),
|
|
FetchTogglesInterval = TimeSpan.FromSeconds(60 * 5),
|
|
InstanceTag = instanceId,
|
|
};
|
|
|
|
api = new DefaultUnleash(settings);
|
|
}
|
|
|
|
return api != null;
|
|
}
|
|
|
|
private static bool CheckFeature(string name, bool defaultValue, AppFeatureContext context)
|
|
{
|
|
return InitializeApi() && api.IsEnabled(name, GetContext(context), defaultValue);
|
|
}
|
|
|
|
private static UnleashContext GetContext(AppFeatureContext ccontext)
|
|
{
|
|
context ??= new();
|
|
context.CurrentTime = DateTime.Now;
|
|
ccontext?.Apply(context);
|
|
return context;
|
|
}
|
|
}
|
|
|
|
public abstract class AppFeatureContext
|
|
{
|
|
public abstract void Apply(UnleashContext context);
|
|
}
|
|
|
|
public class AllowExtrasFeatureContext(ModpackInfo info, ModpackConfig config) : AppFeatureContext
|
|
{
|
|
public override void Apply(UnleashContext context)
|
|
{
|
|
context.UserId = info.ExtrasKey;
|
|
context.Environment = config.Key;
|
|
}
|
|
} |