feat: add configuration model

This commit is contained in:
cswimr 2024-12-31 19:01:25 -05:00
parent 6ef661f2d4
commit e0d744ca5a
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
2 changed files with 47 additions and 2 deletions

18
Models/ZIConfiguration.cs Normal file
View file

@ -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,
}
}

View file

@ -1,9 +1,21 @@
using System.Reflection; using System.Reflection;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using ZenithInfo.Components; using ZenithInfo.Components;
using ZenithInfo.Models;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Configuration
var config = builder.Configuration.GetSection("Configuration").Get<ZIConfiguration>();
builder.Services.Configure<ZIConfiguration>(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. // Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddHttpLogging(o => { }); builder.Services.AddHttpLogging(o => { });
@ -48,8 +60,16 @@ builder.Services.AddSwaggerGen(opts =>
var app = builder.Build(); var app = builder.Build();
app.UseSwagger(); if (config?.UseSwagger == true)
app.UseSwaggerUI(); {
app.UseSwagger();
app.UseSwaggerUI();
}
if (config?.LogRequests == true)
{
app.UseHttpLogging();
}
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
@ -66,4 +86,11 @@ app.UseAntiforgery();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode(); app.MapRazorComponents<App>().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(); app.Run();