diff --git a/src/Web/WebStatus/Program.cs b/src/Web/WebStatus/Program.cs index 53b9ab759..6cfbc1d1e 100644 --- a/src/Web/WebStatus/Program.cs +++ b/src/Web/WebStatus/Program.cs @@ -1,39 +1,67 @@ -var configuration = GetConfiguration(); +try +{ + var builder = WebApplication.CreateBuilder(args); + if (builder.Configuration.GetValue("UseVault", false)) + { + TokenCredential credential = new ClientSecretCredential( + builder.Configuration["Vault:TenantId"], + builder.Configuration["Vault:ClientId"], + builder.Configuration["Vault:ClientSecret"]); + builder.Configuration.AddAzureKeyVault(new Uri($"https://{builder.Configuration["Vault:Name"]}.vault.azure.net/"), credential); + } -Log.Logger = CreateSerilogLogger(configuration); + builder.Host.UseSerilog(CreateLogger(builder.Configuration)); + builder.WebHost.CaptureStartupErrors(false); + builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); + builder.Services.AddApplicationInsightsKubernetesEnricher(); + builder.Services.AddMvc(); + builder.Services.AddOptions(); + builder.Services.AddHealthChecks() + .AddCheck("self", () => HealthCheckResult.Healthy()); + builder.Services + .AddHealthChecksUI() + .AddInMemoryStorage(); + + var app = builder.Build(); + + if (app.Environment.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } -try -{ - Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName); - var host = BuildWebHost(configuration, args); + var pathBase = app.Configuration["PATH_BASE"]; + if (!string.IsNullOrEmpty(pathBase)) + { + app.UsePathBase(pathBase); + } - LogPackagesVersionInfo(); + app.UseHealthChecksUI(config => + { + config.ResourcesPath = string.IsNullOrEmpty(pathBase) ? "/ui/resources" : $"{pathBase}/ui/resources"; + config.UIPath = "/hc-ui"; + }); - Log.Information("Starting web host ({ApplicationContext})...", Program.AppName); - host.Run(); + app.UseStaticFiles(); - return 0; -} -catch (Exception ex) -{ - Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName); - return 1; + app.UseRouting(); + app.MapDefaultControllerRoute(); + app.MapHealthChecks("/liveness", new HealthCheckOptions + { + Predicate = r => r.Name.Contains("self") + }); + + await app.RunAsync(); } -finally +catch (Exception exception) { - Log.CloseAndFlush(); + Console.WriteLine(exception); } -IWebHost BuildWebHost(IConfiguration configuration, string[] args) => - WebHost.CreateDefaultBuilder(args) - .CaptureStartupErrors(false) - .ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) - .UseStartup() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseSerilog() - .Build(); - -Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) +Serilog.ILogger CreateLogger(IConfiguration configuration) { var seqServerUrl = configuration["Serilog:SeqServerUrl"]; var logstashUrl = configuration["Serilog:LogstashgUrl"]; @@ -48,63 +76,8 @@ Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) .CreateLogger(); } -IConfiguration GetConfiguration() -{ - var builder = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddEnvironmentVariables(); - - var config = builder.Build(); - - if (config.GetValue("UseVault", false)) - { - TokenCredential credential = new ClientSecretCredential( - config["Vault:TenantId"], - config["Vault:ClientId"], - config["Vault:ClientSecret"]); - builder.AddAzureKeyVault(new Uri($"https://{config["Vault:Name"]}.vault.azure.net/"), credential); - } - - return builder.Build(); -} - -string GetVersion(Assembly assembly) -{ - try - { - return $"{assembly.GetCustomAttribute()?.Version} ({assembly.GetCustomAttribute()?.InformationalVersion.Split()[0]})"; - } - catch - { - return string.Empty; - } -} - -void LogPackagesVersionInfo() -{ - var assemblies = new List(); - - foreach (var dependencyName in typeof(Program).Assembly.GetReferencedAssemblies()) - { - try - { - // Try to load the referenced assembly... - assemblies.Add(Assembly.Load(dependencyName)); - } - catch - { - // Failed to load assembly. Skip it. - } - } - - var versionList = assemblies.Select(a => $"-{a.GetName().Name} - {GetVersion(a)}").OrderBy(value => value); - - Log.Logger.ForContext("PackageVersions", string.Join("\n", versionList)).Information("Package versions ({ApplicationContext})", Program.AppName); -} - public partial class Program { - private static readonly string _namespace = typeof(Startup).Namespace; + private static readonly string _namespace = typeof(Program).Assembly.GetName().Name; public static readonly string AppName = _namespace; } \ No newline at end of file diff --git a/src/Web/WebStatus/Startup.cs b/src/Web/WebStatus/Startup.cs deleted file mode 100644 index 40e4e4c8b..000000000 --- a/src/Web/WebStatus/Startup.cs +++ /dev/null @@ -1,73 +0,0 @@ -namespace WebStatus; - -public class Startup -{ - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - RegisterAppInsights(services); - - services.AddMvc(); - - services.AddOptions(); - services.AddHealthChecks() - .AddCheck("self", () => HealthCheckResult.Healthy()); - - services - .AddHealthChecksUI() - .AddInMemoryStorage(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - //loggerFactory.AddAzureWebAppDiagnostics(); - //loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - var pathBase = Configuration["PATH_BASE"]; - if (!string.IsNullOrEmpty(pathBase)) - { - app.UsePathBase(pathBase); - } - - app.UseHealthChecksUI(config => - { - config.ResourcesPath = string.IsNullOrEmpty(pathBase) ? "/ui/resources" : $"{pathBase}/ui/resources"; - config.UIPath = "/hc-ui"; - }); - - app.UseStaticFiles(); - - app.UseRouting(); - app.UseEndpoints(endpoints => - { - endpoints.MapDefaultControllerRoute(); - endpoints.MapHealthChecks("/liveness", new HealthCheckOptions - { - Predicate = r => r.Name.Contains("self") - }); - }); - } - - private void RegisterAppInsights(IServiceCollection services) - { - services.AddApplicationInsightsTelemetry(Configuration); - services.AddApplicationInsightsKubernetesEnricher(); - } -}