diff --git a/.gitignore b/.gitignore index 1c626784..585487d8 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ riderModule.iml /ProjectLighthouse/logs/* /ProjectLighthouse/ProjectLighthouse.csproj.user .vs/ +lighthouse.config.json \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/MessageController.cs b/ProjectLighthouse/Controllers/MessageController.cs index 26ec9d3e..beab0d93 100644 --- a/ProjectLighthouse/Controllers/MessageController.cs +++ b/ProjectLighthouse/Controllers/MessageController.cs @@ -4,6 +4,7 @@ using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; +using LBPUnion.ProjectLighthouse.Types.Settings; using Microsoft.AspNetCore.Mvc; namespace LBPUnion.ProjectLighthouse.Controllers @@ -21,11 +22,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers } [HttpGet("eula")] - public async Task Eula() - { - User user = await this.database.UserFromRequest(this.Request); - return user == null ? this.StatusCode(403, "") : this.Ok(EulaHelper.PrivateInstanceNoticeOrBlank + "\n" + $"{EulaHelper.License}\n"); - } + public IActionResult Eula() => this.Ok(ServerSettings.Instance.EulaText + "\n" + $"{EulaHelper.License}\n"); [HttpGet("announce")] public async Task Announce() @@ -33,7 +30,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User user = await this.database.UserFromRequest(this.Request); 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")] diff --git a/ProjectLighthouse/Helpers/EulaHelper.cs b/ProjectLighthouse/Helpers/EulaHelper.cs index bf59ff2c..03c91a38 100644 --- a/ProjectLighthouse/Helpers/EulaHelper.cs +++ b/ProjectLighthouse/Helpers/EulaHelper.cs @@ -1,5 +1,3 @@ -using System.Diagnostics.CodeAnalysis; - namespace LBPUnion.ProjectLighthouse.Helpers { 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 along with this program. If not, see ."; - - 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; } } \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LoggerLevels.cs b/ProjectLighthouse/Logging/LoggerLevels.cs index 2fb28109..3b169f6e 100644 --- a/ProjectLighthouse/Logging/LoggerLevels.cs +++ b/ProjectLighthouse/Logging/LoggerLevels.cs @@ -51,6 +51,12 @@ namespace LBPUnion.ProjectLighthouse.Logging public override string Name => "Photos"; } + public class LoggerLevelConfig : LoggerLevel + { + public static readonly LoggerLevelConfig Instance = new(); + public override string Name => "Config"; + } + public class LoggerLevelAspNet : LoggerLevel { diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index be5bc197..8c2c4e6d 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -28,6 +28,10 @@ namespace LBPUnion.ProjectLighthouse Logger.AddLogger(new LighthouseFileLogger()); 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); bool dbConnected = ServerStatics.DbConnected; Logger.Log(dbConnected ? "Connected to the database." : "Database unavailable! Exiting.", LoggerLevelStartup.Instance); diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs new file mode 100644 index 00000000..7f8c1815 --- /dev/null +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -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(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; } = ""; + } +} \ No newline at end of file