-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
60 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |