Switched to mooncore log rotation. Added experimental https support

This commit is contained in:
Marcel Baumgartner 2024-03-24 12:48:50 +01:00
parent f172d765e3
commit e47a4c29f7
4 changed files with 66 additions and 39 deletions

View file

@ -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";

View file

@ -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;

View file

@ -76,6 +76,7 @@
<Folder Include="Features\FileManager\Http\Resources\" />
<Folder Include="Features\Servers\Http\Resources\" />
<Folder Include="storage\assetOverrides\" />
<Folder Include="storage\logs\" />
<Folder Include="Styles\" />
<Folder Include="wwwroot\css\" />
<Folder Include="wwwroot\js\" />
@ -91,7 +92,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MoonCore" Version="1.1.2" />
<PackageReference Include="MoonCore" Version="1.1.6" />
<PackageReference Include="MoonCoreUI" Version="1.1.4" />
<PackageReference Include="Otp.NET" Version="1.3.0" />
<PackageReference Include="QRCoder" Version="1.4.3" />

View file

@ -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<CoreConfiguration>(
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);