diff --git a/Models/ZIConfiguration.cs b/Models/ZIConfiguration.cs new file mode 100644 index 0000000..444fd4e --- /dev/null +++ b/Models/ZIConfiguration.cs @@ -0,0 +1,18 @@ +namespace ZenithInfo.Models +{ + public class ZIConfiguration + { + public required string GlobalToken { get; init; } + public string? DatabaseConnectionString { get; init; } = null; + public DatabaseTypes DatabaseType { get; init; } = DatabaseTypes.Memory; + public bool UseSentry { get; init; } = true; + public bool UseSwagger { get; init; } = true; + public bool LogRequests { get; init; } = true; + } + + public enum DatabaseTypes + { + Memory, + PostgresSQL, + } +} diff --git a/Program.cs b/Program.cs index 5b26fce..7cf3ccb 100644 --- a/Program.cs +++ b/Program.cs @@ -1,9 +1,21 @@ using System.Reflection; using Microsoft.OpenApi.Models; using ZenithInfo.Components; +using ZenithInfo.Models; var builder = WebApplication.CreateBuilder(args); +// Configuration +var config = builder.Configuration.GetSection("Configuration").Get(); +builder.Services.Configure(builder.Configuration.GetSection("Configuration")); + +if (config?.GlobalToken == "CHANGE-ME") +{ + throw new InvalidOperationException( + "Global token is not a valid value, please set the `CONFIGURATION__GLOBALTOKEN` environment variable to a random value. `openssl rand -hex 64` is a good way to generate a random value." + ); +} + // Add services to the container. builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddHttpLogging(o => { }); @@ -48,8 +60,16 @@ builder.Services.AddSwaggerGen(opts => var app = builder.Build(); -app.UseSwagger(); -app.UseSwaggerUI(); +if (config?.UseSwagger == true) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +if (config?.LogRequests == true) +{ + app.UseHttpLogging(); +} // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) @@ -66,4 +86,11 @@ app.UseAntiforgery(); app.MapRazorComponents().AddInteractiveServerRenderMode(); +if (config?.DatabaseType == DatabaseTypes.Memory) +{ + app.Logger.LogWarning( + "`Memory` database type is in use. Data will be lost when the application is restarted. If you are not in a development environment, you should use `PostgresSQL` instead." + ); +} + app.Run();