mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[PM-1011] LaunchDarkly service (#2726)
* LaunchDarkly service * Load file-based flag values, properly support offline only when self-host * Simplify tests * Use interface for LD settings * Remove tests that will provide inconsistent results depending on machine setup and file fallback * Fall back to offline mode more actively * Drive fallback file path with setting
This commit is contained in:
@ -0,0 +1,54 @@
|
||||
using Bit.Core.Settings;
|
||||
using LaunchDarkly.Sdk.Server;
|
||||
using LaunchDarkly.Sdk.Server.Integrations;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
public class LaunchDarklyFeatureService : IFeatureService, IDisposable
|
||||
{
|
||||
private readonly LdClient _client;
|
||||
|
||||
public LaunchDarklyFeatureService(
|
||||
IGlobalSettings globalSettings)
|
||||
{
|
||||
var ldConfig = Configuration.Builder(globalSettings.LaunchDarkly?.SdkKey);
|
||||
|
||||
if (string.IsNullOrEmpty(globalSettings.LaunchDarkly?.SdkKey))
|
||||
{
|
||||
// support a file to load flag values
|
||||
if (File.Exists(globalSettings.LaunchDarkly?.FlagDataFilePath))
|
||||
{
|
||||
ldConfig.DataSource(
|
||||
FileData.DataSource()
|
||||
.FilePaths(globalSettings.LaunchDarkly?.FlagDataFilePath)
|
||||
.AutoUpdate(true)
|
||||
);
|
||||
|
||||
// do not provide analytics events
|
||||
ldConfig.Events(Components.NoEvents);
|
||||
}
|
||||
else
|
||||
{
|
||||
// when a file-based fallback isn't available, work offline
|
||||
ldConfig.Offline(true);
|
||||
}
|
||||
}
|
||||
else if (globalSettings.SelfHosted)
|
||||
{
|
||||
// when self-hosted, work offline
|
||||
ldConfig.Offline(true);
|
||||
}
|
||||
|
||||
_client = new LdClient(ldConfig.Build());
|
||||
}
|
||||
|
||||
public bool IsOnline()
|
||||
{
|
||||
return _client.Initialized && !_client.IsOffline();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client?.Dispose();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user