mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 10:08:39 +00:00
Add config loading, saving, and upgrading
This commit is contained in:
parent
5de122f1d5
commit
f636fcb3d7
6 changed files with 98 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -12,3 +12,4 @@ riderModule.iml
|
||||||
/ProjectLighthouse/logs/*
|
/ProjectLighthouse/logs/*
|
||||||
/ProjectLighthouse/ProjectLighthouse.csproj.user
|
/ProjectLighthouse/ProjectLighthouse.csproj.user
|
||||||
.vs/
|
.vs/
|
||||||
|
lighthouse.config.json
|
|
@ -4,6 +4,7 @@ using Kettu;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
using LBPUnion.ProjectLighthouse.Logging;
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Controllers
|
namespace LBPUnion.ProjectLighthouse.Controllers
|
||||||
|
@ -21,11 +22,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("eula")]
|
[HttpGet("eula")]
|
||||||
public async Task<IActionResult> Eula()
|
public IActionResult Eula() => this.Ok(ServerSettings.Instance.EulaText + "\n" + $"{EulaHelper.License}\n");
|
||||||
{
|
|
||||||
User user = await this.database.UserFromRequest(this.Request);
|
|
||||||
return user == null ? this.StatusCode(403, "") : this.Ok(EulaHelper.PrivateInstanceNoticeOrBlank + "\n" + $"{EulaHelper.License}\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("announce")]
|
[HttpGet("announce")]
|
||||||
public async Task<IActionResult> Announce()
|
public async Task<IActionResult> Announce()
|
||||||
|
@ -33,7 +30,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
||||||
User user = await this.database.UserFromRequest(this.Request);
|
User user = await this.database.UserFromRequest(this.Request);
|
||||||
if (user == null) return this.StatusCode(403, "");
|
if (user == null) return this.StatusCode(403, "");
|
||||||
|
|
||||||
return this.Ok($"You are now logged in as user {user.Username} (id {user.UserId}).\n\n" + EulaHelper.PrivateInstanceNoticeOrBlank);
|
return this.Ok($"You are now logged in as user {user.Username} (id {user.UserId}).\n\n" + ServerSettings.Instance.EulaText);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("notification")]
|
[HttpGet("notification")]
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Helpers
|
namespace LBPUnion.ProjectLighthouse.Helpers
|
||||||
{
|
{
|
||||||
public static class EulaHelper
|
public static class EulaHelper
|
||||||
|
@ -17,13 +15,5 @@ GNU Affero General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.";
|
along with this program. If not, see <https://www.gnu.org/licenses/>.";
|
||||||
|
|
||||||
public const string PrivateInstanceNotice = @"This server is a private testing instance.
|
|
||||||
Please do not make anything public for now, and keep in mind security isn't as tight as a full release would.";
|
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "HeuristicUnreachableCode")]
|
|
||||||
public const string PrivateInstanceNoticeOrBlank = ShowPrivateInstanceNotice ? PrivateInstanceNotice : "";
|
|
||||||
|
|
||||||
public const bool ShowPrivateInstanceNotice = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -51,6 +51,12 @@ namespace LBPUnion.ProjectLighthouse.Logging
|
||||||
public override string Name => "Photos";
|
public override string Name => "Photos";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class LoggerLevelConfig : LoggerLevel
|
||||||
|
{
|
||||||
|
public static readonly LoggerLevelConfig Instance = new();
|
||||||
|
public override string Name => "Config";
|
||||||
|
}
|
||||||
|
|
||||||
public class LoggerLevelAspNet : LoggerLevel
|
public class LoggerLevelAspNet : LoggerLevel
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,10 @@ namespace LBPUnion.ProjectLighthouse
|
||||||
Logger.AddLogger(new LighthouseFileLogger());
|
Logger.AddLogger(new LighthouseFileLogger());
|
||||||
|
|
||||||
Logger.Log("Welcome to Project Lighthouse!", LoggerLevelStartup.Instance);
|
Logger.Log("Welcome to Project Lighthouse!", LoggerLevelStartup.Instance);
|
||||||
|
|
||||||
|
// This loads the config, see ServerSettings.cs for more information
|
||||||
|
Logger.Log("Loaded config file version " + ServerSettings.Instance.ConfigVersion, LoggerLevelStartup.Instance);
|
||||||
|
|
||||||
Logger.Log("Determining if the database is available...", LoggerLevelStartup.Instance);
|
Logger.Log("Determining if the database is available...", LoggerLevelStartup.Instance);
|
||||||
bool dbConnected = ServerStatics.DbConnected;
|
bool dbConnected = ServerStatics.DbConnected;
|
||||||
Logger.Log(dbConnected ? "Connected to the database." : "Database unavailable! Exiting.", LoggerLevelStartup.Instance);
|
Logger.Log(dbConnected ? "Connected to the database." : "Database unavailable! Exiting.", LoggerLevelStartup.Instance);
|
||||||
|
|
84
ProjectLighthouse/Types/Settings/ServerSettings.cs
Normal file
84
ProjectLighthouse/Types/Settings/ServerSettings.cs
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using Kettu;
|
||||||
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
|
|
||||||
|
namespace LBPUnion.ProjectLighthouse.Types.Settings
|
||||||
|
{
|
||||||
|
[Serializable]
|
||||||
|
public class ServerSettings
|
||||||
|
{
|
||||||
|
static ServerSettings()
|
||||||
|
{
|
||||||
|
if (File.Exists(ConfigFileName))
|
||||||
|
{
|
||||||
|
string configFile = File.ReadAllText(ConfigFileName);
|
||||||
|
|
||||||
|
Instance = JsonSerializer.Deserialize<ServerSettings>(configFile) ?? throw new ArgumentNullException(nameof(ConfigFileName));
|
||||||
|
|
||||||
|
if (Instance.ConfigVersion >= CurrentConfigVersion) return;
|
||||||
|
|
||||||
|
Logger.Log($"Upgrading config file from version {Instance.ConfigVersion} to version {CurrentConfigVersion}", LoggerLevelConfig.Instance);
|
||||||
|
Instance.ConfigVersion = CurrentConfigVersion;
|
||||||
|
configFile = JsonSerializer.Serialize
|
||||||
|
(
|
||||||
|
Instance,
|
||||||
|
typeof(ServerSettings),
|
||||||
|
new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
File.WriteAllText(ConfigFileName, configFile);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string configFile = JsonSerializer.Serialize
|
||||||
|
(
|
||||||
|
new ServerSettings(),
|
||||||
|
typeof(ServerSettings),
|
||||||
|
new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
File.WriteAllText(ConfigFileName, configFile);
|
||||||
|
|
||||||
|
Logger.Log
|
||||||
|
(
|
||||||
|
"The configuration file was not found. " +
|
||||||
|
"A blank configuration file has been created for you at " +
|
||||||
|
$"{Path.Combine(Environment.CurrentDirectory, ConfigFileName)}",
|
||||||
|
LoggerLevelConfig.Instance
|
||||||
|
);
|
||||||
|
|
||||||
|
Environment.Exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Meta
|
||||||
|
|
||||||
|
[NotNull]
|
||||||
|
public static ServerSettings Instance;
|
||||||
|
|
||||||
|
public const int CurrentConfigVersion = 1;
|
||||||
|
|
||||||
|
[JsonPropertyName("ConfigVersionDoNotModifyOrYouWillBeSlapped")]
|
||||||
|
public int ConfigVersion { get; set; } = CurrentConfigVersion;
|
||||||
|
|
||||||
|
public const string ConfigFileName = "lighthouse.config.json";
|
||||||
|
|
||||||
|
#endregion Meta
|
||||||
|
|
||||||
|
public string InfluxOrg { get; set; } = "";
|
||||||
|
public string InfluxBucket { get; set; } = "";
|
||||||
|
public string InfluxToken { get; set; } = "";
|
||||||
|
|
||||||
|
public string EulaText { get; set; } = "";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue