Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 8e8a1ec

Browse files
Simplify AngularCliMiddleware by reducing to a static class. Doesn't affect public API.
1 parent be0d630 commit 8e8a1ec

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/Microsoft.AspNetCore.SpaServices.Extensions/AngularCli/AngularCliMiddleware.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Hosting;
66
using System;
7-
using System.Net.Http;
87
using System.Threading.Tasks;
98
using System.Threading;
109
using Microsoft.AspNetCore.SpaServices.Extensions.Proxy;
@@ -20,17 +19,12 @@
2019

2120
namespace Microsoft.AspNetCore.SpaServices.AngularCli
2221
{
23-
internal class AngularCliMiddleware
22+
internal static class AngularCliMiddleware
2423
{
2524
private const string LogCategoryName = "Microsoft.AspNetCore.SpaServices";
2625
private const int TimeoutMilliseconds = 50 * 1000;
2726

28-
private readonly string _sourcePath;
29-
private readonly ILogger _logger;
30-
private readonly HttpClient _neverTimeOutHttpClient =
31-
ConditionalProxy.CreateHttpClientForProxy(Timeout.InfiniteTimeSpan);
32-
33-
public AngularCliMiddleware(
27+
public static void Attach(
3428
IApplicationBuilder appBuilder,
3529
string sourcePath,
3630
string npmScriptName)
@@ -45,11 +39,9 @@ public AngularCliMiddleware(
4539
throw new ArgumentException("Cannot be null or empty", nameof(npmScriptName));
4640
}
4741

48-
_sourcePath = sourcePath;
49-
_logger = GetOrCreateLogger(appBuilder);
50-
5142
// Start Angular CLI and attach to middleware pipeline
52-
var angularCliServerInfoTask = StartAngularCliServerAsync(npmScriptName);
43+
var logger = GetOrCreateLogger(appBuilder);
44+
var angularCliServerInfoTask = StartAngularCliServerAsync(sourcePath, npmScriptName, logger);
5345

5446
// Everything we proxy is hardcoded to target http://localhost because:
5547
// - the requests are always from the local machine (we're not accepting remote
@@ -61,14 +53,17 @@ public AngularCliMiddleware(
6153
"http", "localhost", task.Result.Port.ToString()));
6254

6355
var applicationStoppingToken = GetStoppingToken(appBuilder);
56+
57+
var neverTimeOutHttpClient =
58+
ConditionalProxy.CreateHttpClientForProxy(Timeout.InfiniteTimeSpan);
6459

6560
// Proxy all requests into the Angular CLI server
6661
appBuilder.Use(async (context, next) =>
6762
{
6863
try
6964
{
7065
var didProxyRequest = await ConditionalProxy.PerformProxyRequest(
71-
context, _neverTimeOutHttpClient, proxyOptionsTask, applicationStoppingToken);
66+
context, neverTimeOutHttpClient, proxyOptionsTask, applicationStoppingToken);
7267

7368
// Since we are proxying everything, this is the end of the middleware pipeline.
7469
// We won't call next().
@@ -100,7 +95,7 @@ internal static ILogger GetOrCreateLogger(IApplicationBuilder appBuilder)
10095
return logger;
10196
}
10297

103-
private void ThrowIfTaskCancelled(Task task)
98+
private static void ThrowIfTaskCancelled(Task task)
10499
{
105100
if (task.IsCanceled)
106101
{
@@ -119,14 +114,15 @@ private static CancellationToken GetStoppingToken(IApplicationBuilder appBuilder
119114
return ((IApplicationLifetime)applicationLifetime).ApplicationStopping;
120115
}
121116

122-
private async Task<AngularCliServerInfo> StartAngularCliServerAsync(string npmScriptName)
117+
private static async Task<AngularCliServerInfo> StartAngularCliServerAsync(
118+
string sourcePath, string npmScriptName, ILogger logger)
123119
{
124120
var portNumber = FindAvailablePort();
125-
_logger.LogInformation($"Starting @angular/cli on port {portNumber}...");
121+
logger.LogInformation($"Starting @angular/cli on port {portNumber}...");
126122

127123
var npmScriptRunner = new NpmScriptRunner(
128-
_sourcePath, npmScriptName, $"--port {portNumber}");
129-
npmScriptRunner.AttachToLogger(_logger);
124+
sourcePath, npmScriptName, $"--port {portNumber}");
125+
npmScriptRunner.AttachToLogger(logger);
130126

131127
Match openBrowserLine;
132128
using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr))

src/Microsoft.AspNetCore.SpaServices.Extensions/AngularCli/AngularCliMiddlewareExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void UseAngularCliServer(
3636
throw new InvalidOperationException($"To use {nameof(UseAngularCliServer)}, you must supply a non-empty value for the {nameof(ISpaOptions.SourcePath)} property of {nameof(ISpaOptions)} when calling {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
3737
}
3838

39-
new AngularCliMiddleware(app, spaOptions.SourcePath, npmScript);
39+
AngularCliMiddleware.Attach(app, spaOptions.SourcePath, npmScript);
4040
}
4141
}
4242
}

0 commit comments

Comments
 (0)