2025-03-11 21:21:16 -05:00
|
|
|
using LANCommander.Server.Services;
|
|
|
|
|
using LANCommander.Server.Services.Models;
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Server.Startup;
|
|
|
|
|
|
|
|
|
|
public static class ApplicationSettings
|
|
|
|
|
{
|
|
|
|
|
public static WebApplicationBuilder AddSettings(this WebApplicationBuilder builder, out Settings settings)
|
|
|
|
|
{
|
2025-08-06 19:37:11 -05:00
|
|
|
// Ensure that our default settings are persisted
|
2025-03-11 21:21:16 -05:00
|
|
|
if (!File.Exists(SettingService.SettingsFile))
|
|
|
|
|
{
|
|
|
|
|
var workingDirectory = Path.GetDirectoryName(SettingService.SettingsFile);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(workingDirectory))
|
|
|
|
|
Directory.CreateDirectory(workingDirectory);
|
|
|
|
|
|
|
|
|
|
settings = new Settings();
|
|
|
|
|
SettingService.SaveSettings(settings);
|
|
|
|
|
}
|
2025-08-06 19:37:11 -05:00
|
|
|
|
|
|
|
|
builder.Configuration.AddYamlFile(SettingService.SettingsFile);
|
|
|
|
|
builder.Services.Configure<Settings>(builder.Configuration);
|
|
|
|
|
|
|
|
|
|
settings = new Settings();
|
|
|
|
|
builder.Configuration.Bind(settings);
|
|
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
Log.Debug("Loading settings");
|
2025-03-11 21:21:16 -05:00
|
|
|
|
|
|
|
|
Log.Debug("Validating settings");
|
|
|
|
|
|
|
|
|
|
if (settings.Authentication.TokenSecret.Length < 16)
|
|
|
|
|
{
|
|
|
|
|
Log.Debug("JWT token secret is too short. Regenerating...");
|
|
|
|
|
settings.Authentication.TokenSecret = Guid.NewGuid().ToString();
|
|
|
|
|
SettingService.SaveSettings(settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Debug("Done validating settings");
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton(settings);
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
}
|