Skip to content

Commit

Permalink
Cleaned up app startup
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingpie committed Nov 24, 2024
1 parent b856761 commit 143b178
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 69 deletions.
7 changes: 7 additions & 0 deletions src/30-Host/Wtq.Host.Base/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;
100 changes: 46 additions & 54 deletions src/30-Host/Wtq.Host.Base/WtqHostBase.cs
Original file line number Diff line number Diff line change
@@ -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.
}
}
3 changes: 2 additions & 1 deletion src/30-Host/Wtq.Host.Linux/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
global using Ardalis.GuardClauses;
global using System.Threading.Tasks;
global using System.Threading.Tasks;
global using Wtq.Utils;
6 changes: 1 addition & 5 deletions src/30-Host/Wtq.Host.Linux/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 1 addition & 3 deletions src/30-Host/Wtq.Host.Linux/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/30-Host/Wtq.Host.Windows/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
global using Ardalis.GuardClauses;
global using System.Threading.Tasks;
global using System.Threading.Tasks;
global using Wtq.Utils;
6 changes: 1 addition & 5 deletions src/30-Host/Wtq.Host.Windows/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 143b178

Please sign in to comment.