diff --git a/src/Stratis.Bitcoin.Features.Api/ApiFeature.cs b/src/Stratis.Bitcoin.Features.Api/ApiFeature.cs index 016e8fe4ca5..33c037acf6e 100644 --- a/src/Stratis.Bitcoin.Features.Api/ApiFeature.cs +++ b/src/Stratis.Bitcoin.Features.Api/ApiFeature.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; @@ -24,8 +25,6 @@ public sealed class ApiFeature : FullNodeFeature private readonly ApiSettings apiSettings; - private readonly ApiFeatureOptions apiFeatureOptions; - private readonly ILogger logger; private IWebHost webHost; @@ -35,14 +34,12 @@ public sealed class ApiFeature : FullNodeFeature public ApiFeature( IFullNodeBuilder fullNodeBuilder, FullNode fullNode, - ApiFeatureOptions apiFeatureOptions, ApiSettings apiSettings, ILoggerFactory loggerFactory, ICertificateStore certificateStore) { this.fullNodeBuilder = fullNodeBuilder; this.fullNode = fullNode; - this.apiFeatureOptions = apiFeatureOptions; this.apiSettings = apiSettings; this.certificateStore = certificateStore; this.logger = loggerFactory.CreateLogger(this.GetType().FullName); @@ -117,8 +114,38 @@ public override void Dispose() } } + /// + /// Options for configuring the full node API feature. + /// public sealed class ApiFeatureOptions { + public ApiFeatureOptions() + { + this.Includes = new List(); + this.Excludes = new List(); + } + + internal IList Includes { get; } + + internal IList Excludes { get; } + + /// + /// Specifies a feature to include when implicitly registering web API controllers. If none are specified, every full node feature is included. + /// + /// The IFullNodeFeature implementation type + public void Include() where T : IFullNodeFeature + { + this.Includes.Add(typeof(T)); + } + + /// + /// Specifies a feature to exclude when implicitly registering web API controllers. + /// + /// The IFullNodeFeature implementation type + public void Exclude() where T : IFullNodeFeature + { + this.Excludes.Add(typeof(T)); + } } /// diff --git a/src/Stratis.Bitcoin.Features.Api/Startup.cs b/src/Stratis.Bitcoin.Features.Api/Startup.cs index b0d96fd981d..8d863028302 100644 --- a/src/Stratis.Bitcoin.Features.Api/Startup.cs +++ b/src/Stratis.Bitcoin.Features.Api/Startup.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Builder; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; @@ -6,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Stratis.Bitcoin.Builder.Feature; using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerUI; @@ -13,10 +16,10 @@ namespace Stratis.Bitcoin.Features.Api { public class Startup { - public Startup(IHostingEnvironment env, IFullNode fullNode) + public Startup(IHostingEnvironment env, IFullNode fullNode, ApiFeatureOptions apiFeatureOptions) { this.fullNode = fullNode; - + this.apiFeatureOptions = apiFeatureOptions; IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) @@ -26,7 +29,8 @@ public Startup(IHostingEnvironment env, IFullNode fullNode) this.Configuration = builder.Build(); } - private IFullNode fullNode; + private readonly IFullNode fullNode; + private readonly ApiFeatureOptions apiFeatureOptions; public IConfigurationRoot Configuration { get; } @@ -55,6 +59,11 @@ public void ConfigureServices(IServiceCollection services) ); }); + // filters api features based on options provided + IEnumerable features = this.apiFeatureOptions.Includes.Any() + ? this.fullNode.Services.Features.Where(feature => this.apiFeatureOptions.Includes.Contains(feature.GetType())) + : this.fullNode.Services.Features.Where(feature => !this.apiFeatureOptions.Excludes.Contains(feature.GetType())); + // Add framework services. services.AddMvc(options => { @@ -69,7 +78,7 @@ public void ConfigureServices(IServiceCollection services) }) // add serializers for NBitcoin objects .AddJsonOptions(options => Utilities.JsonConverters.Serializer.RegisterFrontConverters(options.SerializerSettings)) - .AddControllers(this.fullNode.Services.Features, services); + .AddControllers(features, services); // Enable API versioning. // Note much of this is borrowed from https://github.com/microsoft/aspnet-api-versioning/blob/master/samples/aspnetcore/SwaggerSample/Startup.cs