Add ability to configure environment variables for Node instances, plus auto-populate NODE_ENV based on IHostingEnvironment when possible. Fixes #230

This commit is contained in:
SteveSandersonMS
2016-08-16 16:26:07 -07:00
parent 56cb898bde
commit 098159998d
7 changed files with 62 additions and 24 deletions

View File

@@ -20,12 +20,15 @@ namespace Microsoft.AspNetCore.NodeServices
{
// 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))
{
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
options.ProjectPath = hostEnv.ContentRootPath;
}
// Similarly, we can determine the 'is development' value from the hosting environment
options.AddDefaultEnvironmentVariables(hostEnv.IsDevelopment());
// Likewise, if no logger was specified explicitly, we should use the one from DI.
// If it doesn't provide one, CreateNodeInstance will set up a default.
if (options.NodeInstanceOutputLogger == null)
@@ -69,11 +72,11 @@ namespace Microsoft.AspNetCore.NodeServices
{
case NodeHostingModel.Http:
return new HttpNodeInstance(options.ProjectPath, options.WatchFileExtensions, logger,
options.LaunchWithDebugging, options.DebuggingPort, /* port */ 0);
options.EnvironmentVariables, options.LaunchWithDebugging, options.DebuggingPort, /* port */ 0);
case NodeHostingModel.Socket:
var pipeName = "pni-" + Guid.NewGuid().ToString("D"); // Arbitrary non-clashing string
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions, pipeName, logger,
options.LaunchWithDebugging, options.DebuggingPort);
options.EnvironmentVariables, options.LaunchWithDebugging, options.DebuggingPort);
default:
throw new ArgumentException("Unknown hosting model: " + options.HostingModel);
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.NodeServices.HostingModels;
using Microsoft.Extensions.Logging;
@@ -22,6 +23,23 @@ namespace Microsoft.AspNetCore.NodeServices
public string[] WatchFileExtensions { get; set; }
public ILogger NodeInstanceOutputLogger { get; set; }
public bool LaunchWithDebugging { get; set; }
public IDictionary<string, string> EnvironmentVariables { get; set; }
public int? DebuggingPort { get; set; }
public NodeServicesOptions AddDefaultEnvironmentVariables(bool isDevelopmentMode)
{
if (EnvironmentVariables == null)
{
EnvironmentVariables = new Dictionary<string, string>();
}
if (!EnvironmentVariables.ContainsKey("NODE_ENV"))
{
// These strings are a de-facto standard in Node
EnvironmentVariables["NODE_ENV"] = isDevelopmentMode ? "development" : "production";
}
return this;
}
}
}