|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using Microsoft.AspNetCore.NodeServices; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using Microsoft.AspNetCore.Hosting; |
| 10 | +using System.Threading; |
| 11 | +using Microsoft.AspNetCore.SpaServices.Proxy; |
| 12 | + |
| 13 | +namespace Microsoft.AspNetCore.SpaServices.AngularCli |
| 14 | +{ |
| 15 | + internal class AngularCliMiddleware |
| 16 | + { |
| 17 | + private const string _middlewareResourceName = "/Content/Node/angular-cli-middleware.js"; |
| 18 | + |
| 19 | + private readonly INodeServices _nodeServices; |
| 20 | + private readonly string _middlewareScriptPath; |
| 21 | + |
| 22 | + public AngularCliMiddleware(ISpaBuilder spaBuilder, string sourcePath) |
| 23 | + { |
| 24 | + if (string.IsNullOrEmpty(sourcePath)) |
| 25 | + { |
| 26 | + throw new ArgumentException("Cannot be null or empty", nameof(sourcePath)); |
| 27 | + } |
| 28 | + |
| 29 | + // Prepare to make calls into Node |
| 30 | + var appBuilder = spaBuilder.AppBuilder; |
| 31 | + _nodeServices = CreateNodeServicesInstance(appBuilder, sourcePath); |
| 32 | + _middlewareScriptPath = GetAngularCliMiddlewareScriptPath(appBuilder); |
| 33 | + |
| 34 | + // Start Angular CLI and attach to middleware pipeline |
| 35 | + var angularCliServerInfoTask = StartAngularCliServerAsync(); |
| 36 | + spaBuilder.AddStartupTask(angularCliServerInfoTask); |
| 37 | + |
| 38 | + // Proxy the corresponding requests through ASP.NET and into the Node listener |
| 39 | + // Anything under /<publicpath> (e.g., /dist) is proxied as a normal HTTP request |
| 40 | + // with a typical timeout (100s is the default from HttpClient). |
| 41 | + UseProxyToLocalAngularCliMiddleware(appBuilder, spaBuilder.PublicPath, |
| 42 | + angularCliServerInfoTask, TimeSpan.FromSeconds(100)); |
| 43 | + |
| 44 | + // Advertise the availability of this feature to other SPA middleware |
| 45 | + spaBuilder.Properties.Add(this, null); |
| 46 | + } |
| 47 | + |
| 48 | + public Task StartAngularCliBuilderAsync(string cliAppName) |
| 49 | + { |
| 50 | + return _nodeServices.InvokeExportAsync<AngularCliServerInfo>( |
| 51 | + _middlewareScriptPath, |
| 52 | + "startAngularCliBuilder", |
| 53 | + cliAppName); |
| 54 | + } |
| 55 | + |
| 56 | + private static INodeServices CreateNodeServicesInstance( |
| 57 | + IApplicationBuilder appBuilder, string sourcePath) |
| 58 | + { |
| 59 | + // Unlike other consumers of NodeServices, AngularCliMiddleware dosen't share Node instances, nor does it |
| 60 | + // use your DI configuration. It's important for AngularCliMiddleware to have its own private Node instance |
| 61 | + // because it must *not* restart when files change (it's designed to watch for changes and rebuild). |
| 62 | + var nodeServicesOptions = new NodeServicesOptions(appBuilder.ApplicationServices) |
| 63 | + { |
| 64 | + WatchFileExtensions = new string[] { }, // Don't watch anything |
| 65 | + ProjectPath = Path.Combine(Directory.GetCurrentDirectory(), sourcePath), |
| 66 | + }; |
| 67 | + |
| 68 | + return NodeServicesFactory.CreateNodeServices(nodeServicesOptions); |
| 69 | + } |
| 70 | + |
| 71 | + private static string GetAngularCliMiddlewareScriptPath(IApplicationBuilder appBuilder) |
| 72 | + { |
| 73 | + var script = EmbeddedResourceReader.Read(typeof(AngularCliMiddleware), _middlewareResourceName); |
| 74 | + var nodeScript = new StringAsTempFile(script, GetStoppingToken(appBuilder)); |
| 75 | + return nodeScript.FileName; |
| 76 | + } |
| 77 | + |
| 78 | + private static CancellationToken GetStoppingToken(IApplicationBuilder appBuilder) |
| 79 | + { |
| 80 | + var applicationLifetime = appBuilder |
| 81 | + .ApplicationServices |
| 82 | + .GetService(typeof(IApplicationLifetime)); |
| 83 | + return ((IApplicationLifetime)applicationLifetime).ApplicationStopping; |
| 84 | + } |
| 85 | + |
| 86 | + private async Task<AngularCliServerInfo> StartAngularCliServerAsync() |
| 87 | + { |
| 88 | + // Tell Node to start the server hosting the Angular CLI |
| 89 | + var angularCliServerInfo = await _nodeServices.InvokeExportAsync<AngularCliServerInfo>( |
| 90 | + _middlewareScriptPath, |
| 91 | + "startAngularCliServer"); |
| 92 | + |
| 93 | + // Even after the Angular CLI claims to be listening for requests, there's a short |
| 94 | + // period where it will give an error if you make a request too quickly. Give it |
| 95 | + // a moment to finish starting up. |
| 96 | + await Task.Delay(500); |
| 97 | + |
| 98 | + return angularCliServerInfo; |
| 99 | + } |
| 100 | + |
| 101 | + private static void UseProxyToLocalAngularCliMiddleware( |
| 102 | + IApplicationBuilder appBuilder, string publicPath, |
| 103 | + Task<AngularCliServerInfo> serverInfoTask, TimeSpan requestTimeout) |
| 104 | + { |
| 105 | + // This is hardcoded to use http://localhost because: |
| 106 | + // - the requests are always from the local machine (we're not accepting remote |
| 107 | + // requests that go directly to the Angular CLI middleware server) |
| 108 | + // - given that, there's no reason to use https, and we couldn't even if we |
| 109 | + // wanted to, because in general the Angular CLI server has no certificate |
| 110 | + var proxyOptionsTask = serverInfoTask.ContinueWith( |
| 111 | + task => new ConditionalProxyMiddlewareTarget( |
| 112 | + "http", "localhost", task.Result.Port.ToString())); |
| 113 | + appBuilder.UseMiddleware<ConditionalProxyMiddleware>(publicPath, requestTimeout, proxyOptionsTask); |
| 114 | + } |
| 115 | + |
| 116 | +#pragma warning disable CS0649 |
| 117 | + class AngularCliServerInfo |
| 118 | + { |
| 119 | + public int Port { get; set; } |
| 120 | + } |
| 121 | + } |
| 122 | +#pragma warning restore CS0649 |
| 123 | +} |
0 commit comments