-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (86 loc) · 3.73 KB
/
Program.cs
File metadata and controls
96 lines (86 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using PteroUpdateMonitor.Models;
using PteroUpdateMonitor.Services;
using PteroUpdateMonitor.Workers;
using System.Net.Http.Headers;
using Serilog;
using Serilog.Events;
using PteroUpdateMonitor.Logging;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
Log.Information("Starting application host");
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.Enrich.WithColoredServerName()
.WriteTo.Console(outputTemplate:
"{Timestamp:HH:mm:ss} {ColoredServerName} {Message:lj}{NewLine}{Exception}")
)
.ConfigureServices((hostContext, services) =>
{
var appSettings = hostContext.Configuration.Get<AppSettings>() ?? new AppSettings();
if (appSettings.Servers == null || !appSettings.Servers.Any())
{
var logger = services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
logger.LogCritical("No servers configured in appsettings.json. Exiting.");
var lifetime = services.BuildServiceProvider().GetRequiredService<IHostApplicationLifetime>();
lifetime.StopApplication();
return;
}
services.AddSingleton(appSettings);
services.AddHttpClient("PterodactylApi", client =>
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
});
services.AddHttpClient("Ss14Api", client =>{});
services.AddHttpClient("DiscordWebhook", client =>{});
services.AddSingleton<PterodactylApiService>();
services.AddSingleton<Ss14ApiService>();
services.AddSingleton<DiscordNotificationService>();
foreach (var serverConfig in appSettings.Servers)
{
if (string.IsNullOrWhiteSpace(serverConfig.Name) ||
string.IsNullOrWhiteSpace(serverConfig.ManifestUrl) ||
string.IsNullOrWhiteSpace(serverConfig.ServerIp) ||
string.IsNullOrWhiteSpace(serverConfig.PterodactylApiKey) ||
string.IsNullOrWhiteSpace(serverConfig.PterodactylApiUrl) ||
string.IsNullOrWhiteSpace(serverConfig.PterodactylServerId))
{
var logger = services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
logger.LogWarning("Skipping registration for server '{ServerName}' due to missing essential configuration.", serverConfig.Name ?? "[Unnamed Server]");
continue;
}
services.AddSingleton<IHostedService>(provider => new ServerMonitorWorker(
provider.GetRequiredService<ILogger<ServerMonitorWorker>>(),
serverConfig,
provider.GetRequiredService<Ss14ApiService>(),
provider.GetRequiredService<PterodactylApiService>(),
provider.GetRequiredService<DiscordNotificationService>(),
provider.GetRequiredService<IHostApplicationLifetime>()
));
}
})
.Build();
await host.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}