Create new top-level DefaultNodeInstance concept that will soon hold the "connection draining" logic

This commit is contained in:
SteveSandersonMS
2016-07-06 18:23:25 +01:00
parent 4ee09cbe82
commit 4fb3b18868
19 changed files with 210 additions and 89 deletions

View File

@@ -0,0 +1,58 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.NodeServices.HostingModels;
namespace Microsoft.AspNetCore.NodeServices
{
public static class Configuration
{
public static void AddNodeServices(this IServiceCollection serviceCollection)
=> AddNodeServices(serviceCollection, new NodeServicesOptions());
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeServicesOptions options)
{
serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider =>
{
// Since this instance is being created through DI, we can access the IHostingEnvironment
// to populate options.ProjectPath if it wasn't explicitly specified.
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
if (string.IsNullOrEmpty(options.ProjectPath))
{
options.ProjectPath = hostEnv.ContentRootPath;
}
return new NodeServicesImpl(options, () => CreateNodeInstance(options));
});
}
public static INodeServices CreateNodeServices(NodeServicesOptions options)
{
return new NodeServicesImpl(options, () => CreateNodeInstance(options));
}
private static INodeInstance CreateNodeInstance(NodeServicesOptions options)
{
if (options.NodeInstanceFactory != null)
{
// If you've explicitly supplied an INodeInstance factory, we'll use that. This is useful for
// custom INodeInstance implementations.
return options.NodeInstanceFactory();
}
else
{
// Otherwise we'll construct the type of INodeInstance specified by the HostingModel property,
// which itself has a useful default value.
switch (options.HostingModel)
{
case NodeHostingModel.Http:
return new HttpNodeInstance(options.ProjectPath, /* port */ 0, options.WatchFileExtensions);
case NodeHostingModel.Socket:
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions);
default:
throw new ArgumentException("Unknown hosting model: " + options.HostingModel);
}
}
}
}
}

View File

@@ -0,0 +1,8 @@
namespace Microsoft.AspNetCore.NodeServices
{
public enum NodeHostingModel
{
Http,
Socket,
}
}

View File

@@ -0,0 +1,23 @@
using System;
using Microsoft.AspNetCore.NodeServices.HostingModels;
namespace Microsoft.AspNetCore.NodeServices
{
public class NodeServicesOptions
{
public const NodeHostingModel DefaultNodeHostingModel = NodeHostingModel.Http;
private static readonly string[] DefaultWatchFileExtensions = { ".js", ".jsx", ".ts", ".tsx", ".json", ".html" };
public NodeServicesOptions()
{
HostingModel = DefaultNodeHostingModel;
WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone();
}
public NodeHostingModel HostingModel { get; set; }
public Func<INodeInstance> NodeInstanceFactory { get; set; }
public string ProjectPath { get; set; }
public string[] WatchFileExtensions { get; set; }
}
}