From bfbc5a966ec884fedcedcf4da5424fcddae66f28 Mon Sep 17 00:00:00 2001 From: Alejandro Dominguez Date: Wed, 4 Nov 2020 13:04:47 +0100 Subject: [PATCH] Remove cache function --- README.md | 7 +-- src/Appconfi.Example/Program.cs | 8 +-- src/Appconfi.Test/AppconfiManagerSpec.cs | 16 +++--- .../Controllers/WeatherForecastController.cs | 10 ++-- src/Appconfi.Web.Example/Startup.cs | 9 ++-- src/Appconfi.Web/Appconfi.Web.csproj | 5 +- src/Appconfi.Web/AppconfiOptions.cs | 12 ----- .../AppconfiServiceCollectionExtensions.cs | 24 +-------- src/Appconfi.Web/FeatureManager.cs | 11 ++-- src/Appconfi.Web/IAppconfiFeatureManager.cs | 8 +++ src/Appconfi.Web/IFeatureManager.cs | 7 --- src/Appconfi.Web/ResourceHelper.cs | 49 ----------------- src/Appconfi/AppconfiClient.cs | 2 +- src/Appconfi/AppconfiManager.cs | 52 +++---------------- src/Appconfi/Configuration.cs | 12 +---- src/Appconfi/ConfigurationStore.cs | 4 +- src/Appconfi/IConfigurationManager.cs | 4 +- 17 files changed, 58 insertions(+), 182 deletions(-) create mode 100644 src/Appconfi.Web/IAppconfiFeatureManager.cs delete mode 100644 src/Appconfi.Web/IFeatureManager.cs delete mode 100644 src/Appconfi.Web/ResourceHelper.cs diff --git a/README.md b/README.md index 930a219..615f974 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Appconfi -[Appconfi](https://www.appconfi.com) - Service to centrally manage application settings and feature toggles for applications and services. +[Appconfi] - Service to centrally manage application settings and feature toggles for applications and services. ## Installation @@ -12,7 +12,7 @@ More info is available on [nuget](https://www.nuget.org/packages/Appconfi/) ## Usage -In order to use the Appconfi you will need to [create an account](https://appconfi.com/account/register). +In order to use the Appconfi you will need to deploy an appconfi server and `/account/register`. From there you can create your first application and setup your configuration. To use the Appconfi API to access your configuration go to `/accesskeys` there you can find the `application_id` and your `application_key`. @@ -41,6 +41,3 @@ var refreshInterval = TimeSpan.FromMinutes(1); var manager = Configuration.NewInstance(applicationId, apiKey, env, refreshInterval); ``` -## Links - - * [Web](https://appconfi.com) diff --git a/src/Appconfi.Example/Program.cs b/src/Appconfi.Example/Program.cs index b83b717..231b876 100644 --- a/src/Appconfi.Example/Program.cs +++ b/src/Appconfi.Example/Program.cs @@ -7,12 +7,12 @@ class Program { static void Main(string[] args) { - var applicationId = "530cfb60-da0a-491b-bad8-ff7122100bc1"; - var apiKey = "655759a7573e480f9d727ddf5ae31264"; - var env = "ES"; + var applicationId = ""; + var apiKey = ""; + var env = "[default]"; var manager = Configuration.NewInstance( - new Uri("https://localhost:5001"), + new Uri(""), applicationId, apiKey, env, diff --git a/src/Appconfi.Test/AppconfiManagerSpec.cs b/src/Appconfi.Test/AppconfiManagerSpec.cs index 31e63b2..68ed4e5 100644 --- a/src/Appconfi.Test/AppconfiManagerSpec.cs +++ b/src/Appconfi.Test/AppconfiManagerSpec.cs @@ -118,35 +118,31 @@ public void AppconfiManager_IsFeatureEnabled_ValidKey_ReturnToggle() } [TestMethod] - public void AppconfiManager_IsFeatureEnabled_ValidKey_ReturnFromCache() + public void AppconfiManager_IsFeatureEnabled_ValidKey_ReturnFromDefault() { var configurationStoreMock = new Mock(); var configuration = new ApplicationConfiguration(); //Given a version configurationStoreMock.Setup(x => x.GetVersion()).Returns("1"); - //And given a cache - Func cache = (key) => key == "feature.toggle"; - var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1), null, cache); - var isEnabled = manager.IsFeatureEnabled("feature.toggle"); + var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1)); + var isEnabled = manager.IsFeatureEnabled("feature.toggle", true); Assert.IsTrue(isEnabled); } [TestMethod] - public void AppconfiManager_GetSetting_ValidKey_ReturnFromCache() + public void AppconfiManager_GetSetting_ValidKey_ReturnFromDefault() { var configurationStoreMock = new Mock(); var configuration = new ApplicationConfiguration(); //Given a version configurationStoreMock.Setup(x => x.GetVersion()).Returns("1"); - //And given a cache - Func cache = (key) => key == "cache-setting" ? "value" : "invalid"; - var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1), cache, null); - var value = manager.GetSetting("cache-setting"); + var manager = new AppconfiManager(configurationStoreMock.Object, TimeSpan.FromSeconds(1)); + var value = manager.GetSetting("cache-setting", "value"); Assert.AreEqual("value", value); } diff --git a/src/Appconfi.Web.Example/Controllers/WeatherForecastController.cs b/src/Appconfi.Web.Example/Controllers/WeatherForecastController.cs index 5411654..2099ca1 100644 --- a/src/Appconfi.Web.Example/Controllers/WeatherForecastController.cs +++ b/src/Appconfi.Web.Example/Controllers/WeatherForecastController.cs @@ -16,9 +16,9 @@ public class WeatherForecastController : ControllerBase }; private readonly ILogger _logger; - private readonly IFeatureManager _featureManager; + private readonly IAppconfiFeatureManager _featureManager; - public WeatherForecastController(ILogger logger, IFeatureManager featureManager) + public WeatherForecastController(ILogger logger, IAppconfiFeatureManager featureManager) { _logger = logger; _featureManager = featureManager; @@ -27,8 +27,10 @@ public WeatherForecastController(ILogger logger, IFea [HttpGet] public IEnumerable Get() { - if (!_featureManager.IsEnabled("feature.a")) - return null; + if (!_featureManager.IsEnabled("awesome_feature")) + return Enumerable.Empty() ; + + var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast diff --git a/src/Appconfi.Web.Example/Startup.cs b/src/Appconfi.Web.Example/Startup.cs index 560a436..90a6415 100644 --- a/src/Appconfi.Web.Example/Startup.cs +++ b/src/Appconfi.Web.Example/Startup.cs @@ -27,12 +27,11 @@ public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddAppconfi(options => { - options.Application = "530cfb60-da0a-491b-bad8-ff7122100bc1"; - options.Key = "655759a7573e480f9d727ddf5ae31264"; - options.Environment = "ES"; - options.BaseAddress = "https://localhost:5001"; + options.Application = ""; + options.Key = ""; + options.Environment = ""; + options.BaseAddress = ""; options.CacheExpirationTime = TimeSpan.FromMinutes(1); - options.UseFeatureToggleCache(typeof(Startup).Assembly, "features.json"); }); } diff --git a/src/Appconfi.Web/Appconfi.Web.csproj b/src/Appconfi.Web/Appconfi.Web.csproj index ac8a223..c8b910f 100644 --- a/src/Appconfi.Web/Appconfi.Web.csproj +++ b/src/Appconfi.Web/Appconfi.Web.csproj @@ -3,13 +3,16 @@ netstandard2.1 Appconfi.Web - 1.2.0 + 1.3.0 Appconfi's Team Appconfi Copyright © Appconfi 2020 https://github.com/appconfi/appconfi-csharp LICENSE.txt + Version 1.3.0 + * Remove cache function + * Simplify logic Version 1.2.1 * Adding web extensions diff --git a/src/Appconfi.Web/AppconfiOptions.cs b/src/Appconfi.Web/AppconfiOptions.cs index d5d5392..f26cb27 100644 --- a/src/Appconfi.Web/AppconfiOptions.cs +++ b/src/Appconfi.Web/AppconfiOptions.cs @@ -1,25 +1,13 @@ using System; -using System.Reflection; namespace Appconfi.Web { public class AppconfiOptions { - public AppconfiCacheConfiguration FeatureToggleCacheConfiguration { get; private set; } public string BaseAddress { get; set; } public string Application { get; set; } public string Key { get; set; } public string Environment { get; set; } = "[default]"; public TimeSpan CacheExpirationTime { get; set; } = TimeSpan.FromMinutes(5); - - public AppconfiOptions UseFeatureToggleCache(Assembly assembly, string resourceFileName) - { - FeatureToggleCacheConfiguration = new AppconfiCacheConfiguration - { - Assembly = assembly, - EmbeddedFilePath = resourceFileName - }; - return this; - } } } \ No newline at end of file diff --git a/src/Appconfi.Web/Extensions/DependencyInjection/AppconfiServiceCollectionExtensions.cs b/src/Appconfi.Web/Extensions/DependencyInjection/AppconfiServiceCollectionExtensions.cs index 085513e..424d726 100644 --- a/src/Appconfi.Web/Extensions/DependencyInjection/AppconfiServiceCollectionExtensions.cs +++ b/src/Appconfi.Web/Extensions/DependencyInjection/AppconfiServiceCollectionExtensions.cs @@ -1,28 +1,10 @@ using Microsoft.Extensions.DependencyInjection; using System; -using Newtonsoft.Json; -using System.Collections.Generic; namespace Appconfi.Web.Extensions.DependencyInjection { public static class AppconfiServiceCollectionExtensions { - private static Func GetLocalFeatures(AppconfiCacheConfiguration config) - { - if (config == null) - return null; - - var embeddedResource = ResourceHelper.GetEmbeddedResource(config.EmbeddedFilePath, config.Assembly); - IDictionary cache = JsonConvert.DeserializeObject>(embeddedResource); - Func response = (feature) => { - if (cache.ContainsKey(feature)) - return cache[feature] == "on"; - return false; - }; - - return response; - } - public static IServiceCollection AddAppconfi(this IServiceCollection services, Action configure) { var options = new AppconfiOptions(); @@ -33,13 +15,11 @@ public static IServiceCollection AddAppconfi(this IServiceCollection services, A options.Application, options.Key, options.Environment, - options.CacheExpirationTime, - null, - GetLocalFeatures(options.FeatureToggleCacheConfiguration) + options.CacheExpirationTime ); manager.StartMonitor(); - services.AddSingleton(new FeatureManager(manager)); + services.AddSingleton(new FeatureManager(manager)); return services; } diff --git a/src/Appconfi.Web/FeatureManager.cs b/src/Appconfi.Web/FeatureManager.cs index 5e485a6..cd17438 100644 --- a/src/Appconfi.Web/FeatureManager.cs +++ b/src/Appconfi.Web/FeatureManager.cs @@ -1,6 +1,6 @@ namespace Appconfi.Web { - public class FeatureManager : IFeatureManager + public class FeatureManager : IAppconfiFeatureManager { private readonly AppconfiManager manager; @@ -8,9 +8,14 @@ public FeatureManager(AppconfiManager manager) { this.manager = manager; } - public bool IsEnabled(string featureName) + public bool IsEnabled(string featureName, bool defaultValue = false) { - return manager.IsFeatureEnabled(featureName); + return manager.IsFeatureEnabled(featureName, defaultValue); + } + + public void ForceRefresh() + { + manager.ForceRefresh(); } } } diff --git a/src/Appconfi.Web/IAppconfiFeatureManager.cs b/src/Appconfi.Web/IAppconfiFeatureManager.cs new file mode 100644 index 0000000..e48f505 --- /dev/null +++ b/src/Appconfi.Web/IAppconfiFeatureManager.cs @@ -0,0 +1,8 @@ +namespace Appconfi.Web +{ + public interface IAppconfiFeatureManager + { + void ForceRefresh(); + bool IsEnabled(string featureName, bool defaultValue = false); + } +} \ No newline at end of file diff --git a/src/Appconfi.Web/IFeatureManager.cs b/src/Appconfi.Web/IFeatureManager.cs deleted file mode 100644 index bea4979..0000000 --- a/src/Appconfi.Web/IFeatureManager.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Appconfi.Web -{ - public interface IFeatureManager - { - bool IsEnabled(string featureName); - } -} \ No newline at end of file diff --git a/src/Appconfi.Web/ResourceHelper.cs b/src/Appconfi.Web/ResourceHelper.cs deleted file mode 100644 index e1d67c1..0000000 --- a/src/Appconfi.Web/ResourceHelper.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.IO; -using System.Reflection; - -namespace Appconfi.Web -{ - public static class ResourceHelper - { - public static string GetEmbeddedResource(string resourceName) - { - return GetEmbeddedResource(resourceName, Assembly.GetCallingAssembly()); - } - - public static byte[] GetEmbeddedResourceAsBytes(string resourceName) - { - return GetEmbeddedResourceAsBytes(resourceName, Assembly.GetCallingAssembly()); - } - - public static string GetEmbeddedResource(string resourceName, Assembly assembly) - { - resourceName = FormatResourceName(assembly, resourceName); - using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName)) - { - if (resourceStream == null) - return null; - - using (StreamReader reader = new StreamReader(resourceStream)) - { - return reader.ReadToEnd(); - } - } - } - - public static byte[] GetEmbeddedResourceAsBytes(string resourceName, Assembly assembly) - { - using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName)) - { - byte[] content = new byte[resourceStream.Length]; - resourceStream.Read(content, 0, content.Length); - - return content; - } - } - - private static string FormatResourceName(Assembly assembly, string resourceName) - { - return assembly.GetName().Name + "." + resourceName.Replace(" ", "_").Replace("\\", ".").Replace("/", "."); - } - } -} diff --git a/src/Appconfi/AppconfiClient.cs b/src/Appconfi/AppconfiClient.cs index bbffb90..a457388 100644 --- a/src/Appconfi/AppconfiClient.cs +++ b/src/Appconfi/AppconfiClient.cs @@ -36,7 +36,7 @@ public string PrepareRequest(string resource) queryString["env"] = Environment; queryString["app"] = ApplicationId; - return $"{resource}?{queryString.ToString()}"; + return $"{resource}?{queryString}"; } public string Execute(string resource) diff --git a/src/Appconfi/AppconfiManager.cs b/src/Appconfi/AppconfiManager.cs index cd696c4..efb8d80 100644 --- a/src/Appconfi/AppconfiManager.cs +++ b/src/Appconfi/AppconfiManager.cs @@ -2,11 +2,11 @@ { using System; using System.Collections.Generic; - using System.Diagnostics; using System.Threading; public class AppconfiManager : IDisposable, IConfigurationManager { + private const string FEATURE_ON = "on"; private readonly IConfigurationStore store; readonly TimeSpan interval; private bool monitoring; @@ -18,8 +18,6 @@ public class AppconfiManager : IDisposable, IConfigurationManager IDictionary settingsCache; IDictionary togglesCache; - Func getLocalSetting; - Func getLocalToggle; private readonly ILogger logger; string currentVersion; DateTime lastTimeUpdated; @@ -27,27 +25,14 @@ public class AppconfiManager : IDisposable, IConfigurationManager public AppconfiManager( IConfigurationStore store, TimeSpan interval, - Func getLocalSetting, - Func getLocalToggle, ILogger logger ) { this.logger = logger; - this.getLocalSetting = getLocalSetting; - this.getLocalToggle = getLocalToggle; this.store = store; this.interval = interval; } - public AppconfiManager( - IConfigurationStore store, - TimeSpan interval, - Func getLocalSetting, - Func getLocalToggle - ) : this(store, interval, getLocalSetting, getLocalToggle, null) - { - } - public AppconfiManager( IConfigurationStore store ) : this(store, TimeSpan.FromMinutes(1)) @@ -56,7 +41,7 @@ IConfigurationStore store public AppconfiManager( IConfigurationStore store, - TimeSpan interval): this(store,interval,null, null,null) + TimeSpan interval): this(store,interval,null) { } @@ -123,7 +108,7 @@ public void Dispose() /// /// /// - public string GetSetting(string setting) + public string GetSetting(string setting, string defaultValue = null) { CheckForConfigurationChanges(); @@ -137,7 +122,7 @@ public string GetSetting(string setting) if (settingsCache == null || !settingsCache.TryGetValue(setting, out value)) { - TryGetSettingValueFromLocal(setting, out value); + value = defaultValue; } } finally @@ -153,7 +138,7 @@ public string GetSetting(string setting) /// /// /// - public bool IsFeatureEnabled(string feature) + public bool IsFeatureEnabled(string feature, bool defaultValue= false) { CheckForConfigurationChanges(); @@ -167,9 +152,9 @@ public bool IsFeatureEnabled(string feature) cacheLock.EnterReadLock(); if (togglesCache != null && togglesCache.TryGetValue(feature, out string sv)) - value = sv == "on"; + value = sv == FEATURE_ON; else - TryGetToggleValueFromLocal(feature, out value); + value = defaultValue; } finally { @@ -179,29 +164,6 @@ public bool IsFeatureEnabled(string feature) return value; } - private bool TryGetSettingValueFromLocal(string key, out string value) - { - value = null; - - if (getLocalSetting == null) - return false; - - value = getLocalSetting(key); - return !string.IsNullOrEmpty(value); - } - - private bool TryGetToggleValueFromLocal(string key, out bool value) - { - value = false; - - if (getLocalToggle == null) - return false; - - value = getLocalToggle(key); - - return true; - } - public void ForceRefresh() { CheckForConfigurationChanges(forceRefresh: true); diff --git a/src/Appconfi/Configuration.cs b/src/Appconfi/Configuration.cs index f934a0a..e7b2059 100644 --- a/src/Appconfi/Configuration.cs +++ b/src/Appconfi/Configuration.cs @@ -15,19 +15,15 @@ public static class Configuration /// Configuration environment /// Application logger /// Interval for monitoring the configuration - /// Get setting from local configuration in case of disconnection - /// Get feature toggle from local configuration in case of disconnection public static AppconfiManager NewInstance( string applicationId, string apiKey, string environmentName, TimeSpan updateInterval, - Func getLocalSetting = null, - Func getLocalFeature = null, ILogger logger = null ) { - return NewInstance(AppconfiClient.AppconfiBaseURI, applicationId, apiKey, environmentName, updateInterval, getLocalSetting, getLocalFeature, logger); + return NewInstance(AppconfiClient.AppconfiBaseURI, applicationId, apiKey, environmentName, updateInterval, logger); } /// @@ -39,16 +35,12 @@ public static AppconfiManager NewInstance( /// Configuration environment /// Application logger /// Interval for monitoring the configuration - /// Get setting from local configuration in case of disconnection - /// Get feature toggle from local configuration in case of disconnection public static AppconfiManager NewInstance( Uri appconfiUri, string applicationId, string apiKey, string environmentName, TimeSpan updateInterval, - Func getLocalSetting = null, - Func getLocalFeature = null, ILogger logger = null ) { @@ -58,7 +50,7 @@ public static AppconfiManager NewInstance( var client = new AppconfiClient(appconfiUri, applicationId, apiKey, environmentName); var store = new ConfigurationStore(client); - return new AppconfiManager(store, updateInterval, getLocalSetting, getLocalFeature, logger); + return new AppconfiManager(store, updateInterval, logger); } /// diff --git a/src/Appconfi/ConfigurationStore.cs b/src/Appconfi/ConfigurationStore.cs index 39dac20..f2b4b4d 100644 --- a/src/Appconfi/ConfigurationStore.cs +++ b/src/Appconfi/ConfigurationStore.cs @@ -15,7 +15,7 @@ public ConfigurationStore(AppconfiClient client) public ApplicationConfiguration GetConfiguration() { - var resource = "api/v1/configurations"; + var resource = "api/v1/config"; var request = client.PrepareRequest(resource); var result = client.Execute(request); @@ -41,7 +41,7 @@ ApplicationConfiguration DeserializeConfiguration(string json) public string GetVersion() { - var resource = "api/v1/configurations/version"; + var resource = "api/v1/config/version"; var request = client.PrepareRequest(resource); var result = client.Execute(request); diff --git a/src/Appconfi/IConfigurationManager.cs b/src/Appconfi/IConfigurationManager.cs index 1422f4c..096370d 100644 --- a/src/Appconfi/IConfigurationManager.cs +++ b/src/Appconfi/IConfigurationManager.cs @@ -7,13 +7,13 @@ public interface IConfigurationManager /// /// Setting name /// Value for the given setting - string GetSetting(string key); + string GetSetting(string key, string defaultValue = null); /// /// Determines whether a feature test is enabled for a given environment /// /// Feature name /// If the feature toggle is enabled - bool IsFeatureEnabled(string key); + bool IsFeatureEnabled(string key, bool defaultValue = false); } } \ No newline at end of file