diff --git a/Moonlight/Core/Configuration/CoreConfiguration.cs b/Moonlight/Core/Configuration/CoreConfiguration.cs index a457a4a..3e3fcfd 100644 --- a/Moonlight/Core/Configuration/CoreConfiguration.cs +++ b/Moonlight/Core/Configuration/CoreConfiguration.cs @@ -7,8 +7,10 @@ namespace Moonlight.Core.Configuration; public class CoreConfiguration { [JsonProperty("AppUrl")] - [Description("This defines the public url of moonlight. This will be used by the nodes to communicate with moonlight")] + [Description("This defines the public url of moonlight. This will be used e.g. by the nodes to communicate with moonlight")] public string AppUrl { get; set; } = ""; + + [JsonProperty("Http")] public HttpData Http { get; set; } = new(); [JsonProperty("Database")] public DatabaseData Database { get; set; } = new(); [JsonProperty("Features")] public FeaturesData Features { get; set; } = new(); [JsonProperty("Authentication")] public AuthenticationData Authentication { get; set; } = new(); @@ -16,6 +18,29 @@ public class CoreConfiguration [JsonProperty("Security")] public SecurityData Security { get; set; } = new(); + public class HttpData + { + [Description("The port moonlight should listen to http requests")] + [JsonProperty("HttpPort")] + public int HttpPort { get; set; } = 80; + + [Description("The port moonlight should listen to https requests if ssl is enabled")] + [JsonProperty("HttpsPort")] + public int HttpsPort { get; set; } = 443; + + [Description("Enables the use of an ssl certificate which is required in order to acceppt https requests")] + [JsonProperty("EnableSsl")] + public bool EnableSsl { get; set; } = false; + + [Description("Specifies the location of the certificate .pem file to load")] + [JsonProperty("CertPath")] + public string CertPath { get; set; } = ""; + + [Description("Specifies the location of the key .pem file to load")] + [JsonProperty("KeyPath")] + public string KeyPath { get; set; } = ""; + } + public class DatabaseData { [JsonProperty("Host")] public string Host { get; set; } = "your.db.host"; diff --git a/Moonlight/Core/Implementations/Diagnose/LogDiagnoseAction.cs b/Moonlight/Core/Implementations/Diagnose/LogDiagnoseAction.cs index 0fdacba..1a15340 100644 --- a/Moonlight/Core/Implementations/Diagnose/LogDiagnoseAction.cs +++ b/Moonlight/Core/Implementations/Diagnose/LogDiagnoseAction.cs @@ -9,7 +9,7 @@ public class LogDiagnoseAction : IDiagnoseAction { public async Task GenerateReport(ZipArchive archive, IServiceProvider serviceProvider) { - var path = PathBuilder.File("storage", "logs", "latest.log"); + var path = PathBuilder.File("storage", "logs", "moonlight.log"); if(!File.Exists(path)) return; diff --git a/Moonlight/Moonlight.csproj b/Moonlight/Moonlight.csproj index 6d59382..007b8aa 100644 --- a/Moonlight/Moonlight.csproj +++ b/Moonlight/Moonlight.csproj @@ -76,6 +76,7 @@ + @@ -91,7 +92,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index acb424c..5770dc1 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -1,3 +1,4 @@ +using System.Security.Cryptography.X509Certificates; using MoonCore.Extensions; using MoonCore.Helpers; using MoonCore.Services; @@ -18,50 +19,50 @@ var configService = new ConfigService( var builder = WebApplication.CreateBuilder(args); -// Log rotate -var latestLogPath = PathBuilder.File("storage", "logs", "latest.log"); - -int GetCurrentRotateId() -{ - var counter = 0; - - foreach (var file in Directory.GetFiles(PathBuilder.Dir("storage", "logs"))) - { - var fileName = Path.GetFileName(file); - var fileNameParts = fileName.Split("."); - - if(fileNameParts.Length < 3) - continue; - - var numberPart = fileNameParts[1]; - - if(!int.TryParse(numberPart, out var number)) - continue; - - if (number > counter) - counter = number; - } - - return counter + 1; -} - -if(File.Exists(latestLogPath)) - File.Move(latestLogPath, PathBuilder.File("storage", "logs", $"moonlight.{GetCurrentRotateId()}.log")); - // Setup logging Logger.Setup( logInConsole: true, logInFile: true, - logPath: latestLogPath, - isDebug: builder.Environment.IsDevelopment() + logPath: PathBuilder.File("storage", "logs", "moonlight.log"), + isDebug: builder.Environment.IsDevelopment(), + enableFileLogRotate: true, + rotateLogNameTemplate: PathBuilder.File("storage", "logs", "moonlight.{0}.log") ); builder.Logging.MigrateToMoonCore(); +builder.Logging.AddConfiguration("{\"LogLevel\":{\"Default\":\"Information\",\"Microsoft.AspNetCore\":\"Warning\"}}"); -var config = - new ConfigurationBuilder().AddJsonString( - "{\"LogLevel\":{\"Default\":\"Information\",\"Microsoft.AspNetCore\":\"Warning\"}}"); -builder.Logging.AddConfiguration(config.Build()); +// Configure http +if (builder.Environment.IsDevelopment()) + Logger.Info( + "Disabling http pipeline configuration as the environment is set to development. All http endpoint config options in the core.json will be ignored"); +else +{ + var httpConfig = configService.Get().Http; + + X509Certificate2? certificate = default; + + if (httpConfig.EnableSsl) + { + try + { + certificate = X509Certificate2.CreateFromPemFile(httpConfig.CertPath, httpConfig.KeyPath); + + Logger.Info($"Successfully loaded certificate '{certificate.FriendlyName}'"); + } + catch (Exception e) + { + Logger.Fatal("An error occured while loading certificate"); + Logger.Fatal(e); + } + } + + builder.WebHost.ConfigureMoonCoreHttp( + httpConfig.HttpPort, + httpConfig.EnableSsl, + httpConfig.HttpsPort + ); +} // Build feature service and perform load var featureService = new FeatureService(configService);