From 143b1787dee3406ae3eda13d2496c2d6ef653b0e Mon Sep 17 00:00:00 2001 From: Marco van den Oever <marco@flyingpie.nl> Date: Sun, 24 Nov 2024 18:49:01 +0100 Subject: [PATCH] Cleaned up app startup --- src/30-Host/Wtq.Host.Base/GlobalUsings.cs | 7 ++ src/30-Host/Wtq.Host.Base/WtqHostBase.cs | 100 ++++++++---------- src/30-Host/Wtq.Host.Linux/GlobalUsings.cs | 3 +- src/30-Host/Wtq.Host.Linux/Program.cs | 6 +- .../Properties/launchSettings.json | 4 +- src/30-Host/Wtq.Host.Windows/GlobalUsings.cs | 3 +- src/30-Host/Wtq.Host.Windows/Program.cs | 6 +- 7 files changed, 60 insertions(+), 69 deletions(-) create mode 100644 src/30-Host/Wtq.Host.Base/GlobalUsings.cs diff --git a/src/30-Host/Wtq.Host.Base/GlobalUsings.cs b/src/30-Host/Wtq.Host.Base/GlobalUsings.cs new file mode 100644 index 00000000..499098db --- /dev/null +++ b/src/30-Host/Wtq.Host.Base/GlobalUsings.cs @@ -0,0 +1,7 @@ +global using Microsoft.Extensions.Hosting; +global using Microsoft.Extensions.Logging; +global using System; +global using System.IO; +global using System.Threading.Tasks; +global using Wtq.Configuration; +global using Wtq.Utils; \ No newline at end of file diff --git a/src/30-Host/Wtq.Host.Base/WtqHostBase.cs b/src/30-Host/Wtq.Host.Base/WtqHostBase.cs index b19b5294..fbbb5956 100644 --- a/src/30-Host/Wtq.Host.Base/WtqHostBase.cs +++ b/src/30-Host/Wtq.Host.Base/WtqHostBase.cs @@ -1,83 +1,75 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Serilog; -using System; -using System.IO; -using System.Threading.Tasks; -using Wtq.Configuration; using Wtq.Services.UI; namespace Wtq.Host.Base; public class WtqHostBase { - private readonly IHost _host; - - public WtqHostBase() + public async Task RunAsync(string[] args) { + // Setup logging ASAP, so we can log stuff if initialization goes awry. + Utils.Log.Configure(); + var log = Utils.Log.For(typeof(WtqHostBase)); - // Configuration. - var pathToWtqConf = WtqOptionsPath.Instance.Path; - var config = new ConfigurationBuilder() - .SetBasePath(Path.GetDirectoryName(pathToWtqConf)!) - .AddJsonFile(f => - { - f.ReloadOnChange = true; - f.Optional = false; - f.Path = Path.GetFileName(pathToWtqConf); - f.OnLoadException = x => - { - log.LogError(x.Exception, "Error loading configuration file '{File}': {Message}", pathToWtqConf, x.Exception.Message); - Console.WriteLine($"Error loading configuration file '{pathToWtqConf}': {x.Exception.Message}"); + try + { + // Find path to settings files (wtq.jsonc or similar). + var pathToWtqConf = WtqOptionsPath.Instance.Path; - // MessageBox.Show($"Error loading configuration file '{pathToWtqConf}': {x.Exception.Message}"); - }; - }) - .Build(); + // Load config file. + var config = new ConfigurationBuilder() + .SetBasePath(Path.GetDirectoryName(pathToWtqConf)!) + .AddEnvironmentVariables() + .AddJsonFile(f => + { + f.ReloadOnChange = true; + f.Optional = false; + f.Path = Path.GetFileName(pathToWtqConf); + f.OnLoadException = x => + { + log.LogError(x.Exception, "Error loading configuration file '{File}': {Message}", pathToWtqConf, x.Exception.Message); + }; + }) + .AddCommandLine(args) + .Build(); - _host = new HostBuilder() - .ConfigureAppConfiguration(opt => - { - opt.AddConfiguration(config); - }) - .ConfigureServices(opt => - { - opt - .AddOptionsWithValidateOnStart<WtqOptions>() - .Bind(config); + await new HostBuilder() + .ConfigureAppConfiguration(opt => + { + opt.AddConfiguration(config); + }) + .ConfigureServices(opt => + { + opt + .AddOptionsWithValidateOnStart<WtqOptions>() + .Bind(config); - opt - .AddUI() + opt + .AddUI() - // Utils - .AddWtqCore(); + // Utils + .AddWtqCore(); - ConfigureServices(opt); - }) - .UseSerilog() - .Build(); - } + ConfigureServices(opt); + }) + .UseSerilog() + .Build() - public async Task RunAsync() - { - try - { - await _host + // Run! .RunAsync() - .ConfigureAwait(false); + .NoCtx(); } catch (Exception ex) { - Console.WriteLine($"Error running application: {ex}"); - - // MessageBox.Show($"Error running application: {ex}", "Error starting WTQ"); + log.LogError(ex, "Error running application: {Message}", ex.Message); } } protected virtual void ConfigureServices(IServiceCollection services) { + // Implemented by OS-specific implementations. } } \ No newline at end of file diff --git a/src/30-Host/Wtq.Host.Linux/GlobalUsings.cs b/src/30-Host/Wtq.Host.Linux/GlobalUsings.cs index ae67f9c4..64aa6db3 100644 --- a/src/30-Host/Wtq.Host.Linux/GlobalUsings.cs +++ b/src/30-Host/Wtq.Host.Linux/GlobalUsings.cs @@ -1,2 +1,3 @@ global using Ardalis.GuardClauses; -global using System.Threading.Tasks; \ No newline at end of file +global using System.Threading.Tasks; +global using Wtq.Utils; \ No newline at end of file diff --git a/src/30-Host/Wtq.Host.Linux/Program.cs b/src/30-Host/Wtq.Host.Linux/Program.cs index 32846b6b..054ce232 100644 --- a/src/30-Host/Wtq.Host.Linux/Program.cs +++ b/src/30-Host/Wtq.Host.Linux/Program.cs @@ -1,13 +1,9 @@ -using Wtq.Utils; - namespace Wtq.Host.Linux; public static class Program { public static async Task Main(string[] args) { - Log.Configure(); - - await new WtqLinux().RunAsync().NoCtx(); + await new WtqLinux().RunAsync(args).NoCtx(); } } \ No newline at end of file diff --git a/src/30-Host/Wtq.Host.Linux/Properties/launchSettings.json b/src/30-Host/Wtq.Host.Linux/Properties/launchSettings.json index 9792cf04..9d0536d8 100644 --- a/src/30-Host/Wtq.Host.Linux/Properties/launchSettings.json +++ b/src/30-Host/Wtq.Host.Linux/Properties/launchSettings.json @@ -2,10 +2,8 @@ "profiles": { "Wtq.Host.Linux": { "commandName": "Project", - "workingDirectory": "/home/marco", "environmentVariables": { - "WEBKIT_DISABLE_DMABUF_RENDERER": "1", - "WTQ_CONFIG_FILE": "/home/marco/.config/wtq/wtq.jsonc" + "WEBKIT_DISABLE_DMABUF_RENDERER": "1" } } } diff --git a/src/30-Host/Wtq.Host.Windows/GlobalUsings.cs b/src/30-Host/Wtq.Host.Windows/GlobalUsings.cs index ae67f9c4..64aa6db3 100644 --- a/src/30-Host/Wtq.Host.Windows/GlobalUsings.cs +++ b/src/30-Host/Wtq.Host.Windows/GlobalUsings.cs @@ -1,2 +1,3 @@ global using Ardalis.GuardClauses; -global using System.Threading.Tasks; \ No newline at end of file +global using System.Threading.Tasks; +global using Wtq.Utils; \ No newline at end of file diff --git a/src/30-Host/Wtq.Host.Windows/Program.cs b/src/30-Host/Wtq.Host.Windows/Program.cs index e1297763..dafa24e4 100644 --- a/src/30-Host/Wtq.Host.Windows/Program.cs +++ b/src/30-Host/Wtq.Host.Windows/Program.cs @@ -1,13 +1,9 @@ -using Wtq.Utils; - namespace Wtq.Host.Windows; public static class Program { public static async Task Main(string[] args) { - Log.Configure(); - - await new WtqWin32().RunAsync().NoCtx(); + await new WtqWin32().RunAsync(args).NoCtx(); } } \ No newline at end of file