Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Implicit controller registration selection #4126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/Stratis.Bitcoin.Features.Api/ApiFeature.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -24,8 +25,6 @@ public sealed class ApiFeature : FullNodeFeature

private readonly ApiSettings apiSettings;

private readonly ApiFeatureOptions apiFeatureOptions;

private readonly ILogger logger;

private IWebHost webHost;
Expand All @@ -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);
Expand Down Expand Up @@ -117,8 +114,38 @@ public override void Dispose()
}
}

/// <summary>
/// Options for configuring the full node API feature.
/// </summary>
public sealed class ApiFeatureOptions
{
public ApiFeatureOptions()
{
this.Includes = new List<Type>();
this.Excludes = new List<Type>();
}

internal IList<Type> Includes { get; }

internal IList<Type> Excludes { get; }

/// <summary>
/// Specifies a feature to include when implicitly registering web API controllers. If none are specified, every full node feature is included.
/// </summary>
/// <typeparam name="T">The IFullNodeFeature implementation type</typeparam>
public void Include<T>() where T : IFullNodeFeature
{
this.Includes.Add(typeof(T));
}

/// <summary>
/// Specifies a feature to exclude when implicitly registering web API controllers.
/// </summary>
/// <typeparam name="T">The IFullNodeFeature implementation type</typeparam>
public void Exclude<T>() where T : IFullNodeFeature
{
this.Excludes.Add(typeof(T));
}
}

/// <summary>
Expand Down
19 changes: 14 additions & 5 deletions src/Stratis.Bitcoin.Features.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
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;
using Microsoft.Extensions.Configuration;
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;

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)
Expand All @@ -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; }

Expand Down Expand Up @@ -55,6 +59,11 @@ public void ConfigureServices(IServiceCollection services)
);
});

// filters api features based on options provided
IEnumerable<IFullNodeFeature> 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 =>
{
Expand All @@ -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
Expand Down