From eec0a71371c9c4671727fecd225672f6590e5ccb Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 8 Mar 2021 12:29:13 -0800 Subject: [PATCH] WIP --- src/Microsoft.Tye.Hosting/HttpProxyService.cs | 14 +- .../Microsoft.Tye.Hosting.csproj | 1 - src/Microsoft.Tye.Hosting/TyeHost.cs | 165 +++++++++--------- test/E2ETest/ReplicaStoppingTests.cs | 4 +- test/E2ETest/TyeRunTests.cs | 4 +- test/Test.Infrastructure/TestHelpers.cs | 2 +- 6 files changed, 97 insertions(+), 93 deletions(-) diff --git a/src/Microsoft.Tye.Hosting/HttpProxyService.cs b/src/Microsoft.Tye.Hosting/HttpProxyService.cs index cc22c424..28915185 100644 --- a/src/Microsoft.Tye.Hosting/HttpProxyService.cs +++ b/src/Microsoft.Tye.Hosting/HttpProxyService.cs @@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Proxy; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Matching; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Tye.Hosting.Model; @@ -24,7 +25,7 @@ namespace Microsoft.Tye.Hosting { public partial class HttpProxyService : IApplicationProcessor { - private List _webApplications = new List(); + private List _webApplications = new List(); private readonly ILogger _logger; private ConcurrentDictionary _readyPorts; @@ -50,11 +51,16 @@ namespace Microsoft.Tye.Hosting if (service.Description.RunInfo is IngressRunInfo runInfo) { + var host = Host.CreateDefaultBuilder() + .ConfigureWebHostDefaults(builder => + { + builder.Configure(app => + { + + }); + }); var builder = new WebApplicationBuilder(); - builder.Services.AddSingleton(); - - builder.Logging.AddProvider(new ServiceLoggerProvider(service.Logs)); var addresses = new List(); diff --git a/src/Microsoft.Tye.Hosting/Microsoft.Tye.Hosting.csproj b/src/Microsoft.Tye.Hosting/Microsoft.Tye.Hosting.csproj index 9e26f6f2..0d84a891 100644 --- a/src/Microsoft.Tye.Hosting/Microsoft.Tye.Hosting.csproj +++ b/src/Microsoft.Tye.Hosting/Microsoft.Tye.Hosting.csproj @@ -20,7 +20,6 @@ - diff --git a/src/Microsoft.Tye.Hosting/TyeHost.cs b/src/Microsoft.Tye.Hosting/TyeHost.cs index f073e441..58295f61 100644 --- a/src/Microsoft.Tye.Hosting/TyeHost.cs +++ b/src/Microsoft.Tye.Hosting/TyeHost.cs @@ -5,13 +5,15 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.Linq; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; @@ -32,6 +34,7 @@ namespace Microsoft.Tye.Hosting private Microsoft.Extensions.Logging.ILogger? _logger; private IHostApplicationLifetime? _lifetime; + private ICollection? _addresses; private AggregateApplicationProcessor? _processor; private readonly Application _application; @@ -46,7 +49,11 @@ namespace Microsoft.Tye.Hosting public Application Application => _application; - public WebApplication? DashboardWebApplication { get; set; } + public IHost? DashboardWebApplication { get; set; } + + public ICollection? Addresses => _addresses; + + public Extensions.Logging.ILogger? Logger => _logger; // An additional sink that output will be piped to. Useful for testing. public ILogEventSink? Sink { get; set; } @@ -71,25 +78,24 @@ namespace Microsoft.Tye.Hosting } } - public async Task StartAsync() + public async Task StartAsync() { var app = BuildWebApplication(_application, _options, Sink); DashboardWebApplication = app; - _logger = app.Logger; - _lifetime = app.ApplicationLifetime; + _addresses = DashboardWebApplication.Services.GetRequiredService().Features.Get().Addresses; + _logger = DashboardWebApplication.Services.GetRequiredService>(); + _lifetime = app.Services.GetRequiredService(); _logger.LogInformation("Executing application from {Source}", _application.Source); - ConfigureApplication(app); - _replicaRegistry = new ReplicaRegistry(_application.ContextDirectory, _logger); _processor = CreateApplicationProcessor(_replicaRegistry, _options, _logger); await app.StartAsync(); - _logger.LogInformation("Dashboard running on {Address}", app.Addresses.First()); + _logger.LogInformation("Dashboard running on {Address}", _addresses.First()); try { @@ -103,88 +109,79 @@ namespace Microsoft.Tye.Hosting if (_options.Dashboard) { - OpenDashboard(app.Addresses.First()); + OpenDashboard(_addresses.First()); } return app; } - private static WebApplication BuildWebApplication(Application application, HostOptions options, ILogEventSink? sink) + private IHost BuildWebApplication(Application application, HostOptions options, ILogEventSink? sink) { - var args = new List(); - if (options.Port.HasValue) - { - args.Add("--port"); - args.Add(options.Port.Value.ToString(CultureInfo.InvariantCulture)); - } - - var builder = WebApplication.CreateBuilder(args.ToArray()); - - // Logging for this application - builder.Host.UseSerilog((context, configuration) => - { - var logLevel = options.LogVerbosity switch + return Host.CreateDefaultBuilder() + .UseSerilog((context, configuration) => { - Verbosity.Quiet => LogEventLevel.Warning, - Verbosity.Info => LogEventLevel.Information, - Verbosity.Debug => LogEventLevel.Verbose, - _ => default - }; + var logLevel = options.LogVerbosity switch + { + Verbosity.Quiet => LogEventLevel.Warning, + Verbosity.Info => LogEventLevel.Information, + Verbosity.Debug => LogEventLevel.Verbose, + _ => default + }; - configuration - .MinimumLevel.Is(logLevel) - .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore")) - .Filter.ByExcluding(Matching.FromSource("Microsoft.Extensions")) - .Filter.ByExcluding(Matching.FromSource("Microsoft.Hosting")) - .Enrich - .FromLogContext() - .WriteTo - .Console(); + configuration + .MinimumLevel.Is(logLevel) + .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore")) + .Filter.ByExcluding(Matching.FromSource("Microsoft.Extensions")) + .Filter.ByExcluding(Matching.FromSource("Microsoft.Hosting")) + .Enrich + .FromLogContext() + .WriteTo + .Console(); - if (sink is object) + if (sink is object) + { + configuration.WriteTo.Sink(sink, logLevel); + } + }) + .ConfigureServices(services => { - configuration.WriteTo.Sink(sink, logLevel); - } - }); + services.AddRazorPages(o => o.RootDirectory = "/Dashboard/Pages"); + services.AddServerSideBlazor(); + services.AddOptions() + .PostConfigure(o => + { + var fileProvider = new ManifestEmbeddedFileProvider(typeof(TyeHost).Assembly, "wwwroot"); - builder.Services.AddRazorPages(o => o.RootDirectory = "/Dashboard/Pages"); - - builder.Services.AddServerSideBlazor(); - - builder.Services.AddOptions() - .PostConfigure(o => + // Make sure we don't remove the existing file providers (blazor needs this) + o.FileProvider = new CompositeFileProvider(o.FileProvider, fileProvider); + }); + services.AddCors( + options => + { + options.AddPolicy( + "default", + policy => + { + policy + .AllowAnyOrigin() + .AllowAnyHeader() + .AllowAnyMethod(); + }); + }); + services.AddSingleton(application); + }) + .ConfigureWebHostDefaults(builder => { - var fileProvider = new ManifestEmbeddedFileProvider(typeof(TyeHost).Assembly, "wwwroot"); + var port = ComputePort(options.Port); - // Make sure we don't remove the existing file providers (blazor needs this) - o.FileProvider = new CompositeFileProvider(o.FileProvider, fileProvider); - }); - - builder.Services.AddCors( - options => - { - options.AddPolicy( - "default", - policy => - { - policy - .AllowAnyOrigin() - .AllowAnyHeader() - .AllowAnyMethod(); - }); - }); - - builder.Services.AddSingleton(application); - var app = builder.Build(); - return app; + builder.Configure(ConfigureApplication) + .UseUrls($"http://127.0.0.1:{port}"); + }) + .Build(); } - private void ConfigureApplication(WebApplication app) + private void ConfigureApplication(IApplicationBuilder app) { - var port = ComputePort(app); - - app.Listen($"http://127.0.0.1:{port}"); - app.UseDeveloperExceptionPage(); app.UseCors("default"); @@ -195,13 +192,16 @@ namespace Microsoft.Tye.Hosting var api = new TyeDashboardApi(); - api.MapRoutes(app); + app.UseEndpoints(endpoints => + { + api.MapRoutes(endpoints); - app.MapBlazorHub(); - app.MapFallbackToPage("/_Host"); + endpoints.MapBlazorHub(); + endpoints.MapFallbackToPage("/_Host"); + }); } - private int ComputePort(WebApplication app) + private int ComputePort(int? port) { // logic for computing the port: // - we allow the user to specify the port... if they don't @@ -210,17 +210,16 @@ namespace Microsoft.Tye.Hosting // - we don't want to cause conflicts with any of the users known bindings // or something else running. - var port = app.Configuration["port"]; - if (!string.IsNullOrEmpty(port)) + if (port.HasValue) { // Port was passed in at the command-line, use it! - return int.Parse(port, NumberStyles.Number, CultureInfo.InvariantCulture); + return port.Value; } if (IsPortInUseByBinding(_application, DefaultPort)) { // Port has been reserved for the app. - app.Logger.LogInformation("Default dashboard port {DefaultPort} has been reserved by the application, choosing random port.", DefaultPort); + // app.Logger.LogInformation("Default dashboard port {DefaultPort} has been reserved by the application, choosing random port.", DefaultPort); return AutodetectPort; } @@ -229,7 +228,7 @@ namespace Microsoft.Tye.Hosting return DefaultPort; } // Port is in use by something already running. - app.Logger.LogInformation("Default dashboard port {DefaultPort} is in use, choosing random port.", DefaultPort); + // app.Logger.LogInformation("Default dashboard port {DefaultPort} is in use, choosing random port.", DefaultPort); return AutodetectPort; } diff --git a/test/E2ETest/ReplicaStoppingTests.cs b/test/E2ETest/ReplicaStoppingTests.cs index db2e56d7..6ba8aa4e 100644 --- a/test/E2ETest/ReplicaStoppingTests.cs +++ b/test/E2ETest/ReplicaStoppingTests.cs @@ -112,7 +112,7 @@ namespace E2ETest { await StartHostAndWaitForReplicasToStart(host); - var uri = new Uri(host.DashboardWebApplication!.Addresses.First()); + var uri = new Uri(host.Addresses!.First()); await execute(host, uri!); } @@ -120,7 +120,7 @@ namespace E2ETest { if (host.DashboardWebApplication != null) { - var uri = new Uri(host.DashboardWebApplication!.Addresses.First()); + var uri = new Uri(host.Addresses!.First()); using var client = new HttpClient(); diff --git a/test/E2ETest/TyeRunTests.cs b/test/E2ETest/TyeRunTests.cs index 406500de..873b8b9e 100644 --- a/test/E2ETest/TyeRunTests.cs +++ b/test/E2ETest/TyeRunTests.cs @@ -1105,7 +1105,7 @@ services: { await StartHostAndWaitForReplicasToStart(host); - var uri = new Uri(host.DashboardWebApplication!.Addresses.First()); + var uri = new Uri(host.Addresses!.First()); await execute(host.Application, uri!); } @@ -1113,7 +1113,7 @@ services: { if (host.DashboardWebApplication != null) { - var uri = new Uri(host.DashboardWebApplication!.Addresses.First()); + var uri = new Uri(host.Addresses!.First()); using var client = new HttpClient(); diff --git a/test/Test.Infrastructure/TestHelpers.cs b/test/Test.Infrastructure/TestHelpers.cs index 51880b9c..0ae89e2c 100644 --- a/test/Test.Infrastructure/TestHelpers.cs +++ b/test/Test.Infrastructure/TestHelpers.cs @@ -246,7 +246,7 @@ namespace Test.Infrastructure { static async Task Purge(TyeHost host) { - var logger = host.DashboardWebApplication!.Logger; + var logger = host.Logger; var replicaRegistry = new ReplicaRegistry(host.Application.ContextDirectory, logger); var processRunner = new ProcessRunner(logger, replicaRegistry, new ProcessRunnerOptions()); var dockerRunner = new DockerRunner(logger, replicaRegistry);