44using Microsoft . AspNetCore . Builder ;
55using Microsoft . AspNetCore . Hosting ;
66using System ;
7- using System . Net . Http ;
87using System . Threading . Tasks ;
98using System . Threading ;
109using Microsoft . AspNetCore . SpaServices . Extensions . Proxy ;
2019
2120namespace 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 ) )
0 commit comments