Compare commits

...

5 commits

9 changed files with 160 additions and 40 deletions

9
.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,9 @@
{
"recommendations": [
"ms-dotnettools.csharp",
"ms-dotnettools.vscodeintellicode-csharp",
"csharpier.csharpier-vscode",
"patcx.vscode-nuget-gallery",
"vasubasraj.flashpost"
]
}

10
.vscode/settings.json vendored
View file

@ -1,3 +1,11 @@
{ {
"dotnet.defaultSolution": "ZenithInfo.sln" "dotnet.defaultSolution": "ZenithInfo.sln",
"[csharp]": {
"editor.defaultFormatter": "csharpier.csharpier-vscode",
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file"
},
"files.associations": {
"*.json": "jsonc"
}
} }

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,27 @@
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."
);
}
// Sentry
if (config?.UseSentry == true)
{
builder.WebHost.UseSentry();
}
// 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 +66,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 +92,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();

View file

@ -1,38 +1,41 @@
{ {
"$schema": "http://json.schemastore.org/launchsettings.json", "$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": { "iisSettings": {
"windowsAuthentication": false, "windowsAuthentication": false,
"anonymousAuthentication": true, "anonymousAuthentication": true,
"iisExpress": { "iisExpress": {
"applicationUrl": "http://localhost:7739", "applicationUrl": "http://localhost:48070",
"sslPort": 44309 "sslPort": 44382
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5150",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
} }
}, },
"profiles": { "https": {
"http": { "commandName": "Project",
"commandName": "Project", "dotnetRunMessages": true,
"dotnetRunMessages": true, "launchBrowser": true,
"launchBrowser": true, "launchUrl": "swagger",
"applicationUrl": "http://localhost:5123", "applicationUrl": "https://localhost:7047;http://localhost:5150",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
}, },
"https": { "IIS Express": {
"commandName": "Project", "commandName": "IISExpress",
"dotnetRunMessages": true, "launchBrowser": true,
"launchBrowser": true, "launchUrl": "swagger",
"applicationUrl": "https://localhost:7268;http://localhost:5123", "environmentVariables": {
"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
} }
} }
} }
}

View file

@ -0,0 +1,26 @@
using System.Security.Cryptography;
using System.Text;
namespace ZenithInfo.Util;
public class RandomStringGenerator
{
private static readonly char[] chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+?".ToCharArray();
public static string GenerateRandomString(int length)
{
var stringBuilder = new StringBuilder();
using (var rng = RandomNumberGenerator.Create())
{
byte[] buffer = new byte[1];
for (int i = 0; i < length; i++)
{
rng.GetBytes(buffer);
var randomIndex = buffer[0] % chars.Length;
stringBuilder.Append(chars[randomIndex]);
}
}
return stringBuilder.ToString();
}
}

View file

@ -10,6 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Sentry.AspNetCore" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
</ItemGroup> </ItemGroup>

View file

@ -1,8 +1,12 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Trace"
"Microsoft.AspNetCore": "Warning" }
} },
"Configuration": {
// DO NOT USE THIS AUTHTOKEN IN PRODUCTION!!!
"AuthToken": "hi",
"UseSentry": false
} }
} }

View file

@ -2,8 +2,26 @@
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"Configuration": {
"AuthToken": "CHANGE-ME",
"UseSwagger": true,
"LogRequests": true,
"UseSentry": true
},
"Sentry": {
"Dsn": "https://92b5c863371d9362c50a39b3faccd26d@sentry.csw.im/5",
"SendDefaultPii": true,
"MaxRequestBodySize": "Always",
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"AttachStackTrace": true,
"Debug": true,
"DiagnosticLevel": "Error",
"TracesSampleRate": 1.0
}
} }