From 06a791831852803b62b030944534e77a3d28bb2e Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 6 Jan 2024 19:04:11 +0100 Subject: [PATCH] Add proper configuration management --- .gitignore | 1 + .../Controllers/ActivityPubController.cs | 3 +- .../Controllers/AuthController.cs | 3 +- .../Controllers/UserController.cs | 6 ++-- .../Core/Database/DatabaseContext.cs | 19 +++++++--- Iceshrimp.Backend/Iceshrimp.Backend.csproj | 35 ++++++++++--------- Iceshrimp.Backend/Startup.cs | 7 ++-- .../appsettings.Development.json | 8 ----- Iceshrimp.Backend/appsettings.json | 9 ----- Iceshrimp.Backend/configuration.ini | 11 ++++++ 10 files changed, 54 insertions(+), 48 deletions(-) delete mode 100644 Iceshrimp.Backend/appsettings.Development.json delete mode 100644 Iceshrimp.Backend/appsettings.json create mode 100644 Iceshrimp.Backend/configuration.ini diff --git a/.gitignore b/.gitignore index 19fe6dce..44693666 100644 --- a/.gitignore +++ b/.gitignore @@ -91,6 +91,7 @@ fabric.properties # End of https://www.toptal.com/developers/gitignore/api/dotnetcore,rider +Iceshrimp.Backend/configuration.overrides.ini Iceshrimp.Backend/wwwroot/frontend Iceshrimp.Backend/wwwroot/.vite Iceshrimp.Frontend/.yarn diff --git a/Iceshrimp.Backend/Controllers/ActivityPubController.cs b/Iceshrimp.Backend/Controllers/ActivityPubController.cs index d3b11f2f..f83e6e94 100644 --- a/Iceshrimp.Backend/Controllers/ActivityPubController.cs +++ b/Iceshrimp.Backend/Controllers/ActivityPubController.cs @@ -29,8 +29,7 @@ public class ActivityPubController : Controller { [HttpGet("/users/{id}")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ASActor))] [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))] - public async Task GetUser(string id) { - var db = new DatabaseContext(); + public async Task GetUser(string id, [FromServices] DatabaseContext db) { var user = await db.Users.FirstOrDefaultAsync(p => p.Id == id); if (user == null) return NotFound(); var rendered = await ActivityPubUserRenderer.Render(user); diff --git a/Iceshrimp.Backend/Controllers/AuthController.cs b/Iceshrimp.Backend/Controllers/AuthController.cs index 5600ac39..2aa00aa3 100644 --- a/Iceshrimp.Backend/Controllers/AuthController.cs +++ b/Iceshrimp.Backend/Controllers/AuthController.cs @@ -27,8 +27,7 @@ public class AuthController : Controller { [ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(ErrorResponse))] [SuppressMessage("Performance", "CA1862:Use the \'StringComparison\' method overloads to perform case-insensitive string comparisons")] - public async Task Login([FromBody] AuthRequest request) { - var db = new DatabaseContext(); + public async Task Login([FromBody] AuthRequest request, [FromServices] DatabaseContext db) { var user = await db.Users.FirstOrDefaultAsync(p => p.UsernameLower == request.Username.ToLowerInvariant() && p.Host == null); if (user == null) return Unauthorized(); diff --git a/Iceshrimp.Backend/Controllers/UserController.cs b/Iceshrimp.Backend/Controllers/UserController.cs index 0ef668e1..df83181c 100644 --- a/Iceshrimp.Backend/Controllers/UserController.cs +++ b/Iceshrimp.Backend/Controllers/UserController.cs @@ -13,8 +13,7 @@ public class UserController : Controller { [HttpGet] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserResponse))] [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))] - public async Task GetUser(string id) { - var db = new DatabaseContext(); + public async Task GetUser(string id, [FromServices] DatabaseContext db) { var user = await db.Users.FirstOrDefaultAsync(p => p.Id == id); if (user == null) return NotFound(); return Ok(user); @@ -23,8 +22,7 @@ public class UserController : Controller { [HttpGet("notes")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(TimelineResponse))] [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))] - public async Task GetUserNotes(string id) { - var db = new DatabaseContext(); + public async Task GetUserNotes(string id, [FromServices] DatabaseContext db) { var user = await db.Users.FirstOrDefaultAsync(p => p.Id == id); if (user == null) return NotFound(); diff --git a/Iceshrimp.Backend/Core/Database/DatabaseContext.cs b/Iceshrimp.Backend/Core/Database/DatabaseContext.cs index 0c563f0e..eb48401d 100644 --- a/Iceshrimp.Backend/Core/Database/DatabaseContext.cs +++ b/Iceshrimp.Backend/Core/Database/DatabaseContext.cs @@ -5,10 +5,14 @@ using Npgsql; namespace Iceshrimp.Backend.Core.Database; public partial class DatabaseContext : DbContext { - public DatabaseContext() { } + private readonly IConfiguration _config; - public DatabaseContext(DbContextOptions options) - : base(options) { } + public DatabaseContext() {} + + public DatabaseContext(DbContextOptions options, IConfiguration config) + : base(options) { + _config = config; + } public virtual DbSet AbuseUserReports { get; init; } @@ -210,7 +214,14 @@ public partial class DatabaseContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //TODO: load from configuration - var dataSourceBuilder = new NpgsqlDataSourceBuilder("Host=localhost;Username=zotan;Database=iceshrimp"); + var dataSourceBuilder = new NpgsqlDataSourceBuilder(); + + var config = _config.GetSection("Database"); + dataSourceBuilder.ConnectionStringBuilder.Host = config["Host"]; + dataSourceBuilder.ConnectionStringBuilder.Username = config["Username"]; + dataSourceBuilder.ConnectionStringBuilder.Password = config["Password"]; + dataSourceBuilder.ConnectionStringBuilder.Database = config["Database"]; + dataSourceBuilder.MapEnum(); dataSourceBuilder.MapEnum(); dataSourceBuilder.MapEnum(); diff --git a/Iceshrimp.Backend/Iceshrimp.Backend.csproj b/Iceshrimp.Backend/Iceshrimp.Backend.csproj index 9fa0cb8f..5e194461 100644 --- a/Iceshrimp.Backend/Iceshrimp.Backend.csproj +++ b/Iceshrimp.Backend/Iceshrimp.Backend.csproj @@ -8,33 +8,34 @@ - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + + - - - - - + + + + + - - + + diff --git a/Iceshrimp.Backend/Startup.cs b/Iceshrimp.Backend/Startup.cs index c43da96c..e1198b88 100644 --- a/Iceshrimp.Backend/Startup.cs +++ b/Iceshrimp.Backend/Startup.cs @@ -4,6 +4,10 @@ using Vite.AspNetCore.Extensions; var builder = WebApplication.CreateBuilder(args); +builder.Configuration.Sources.Clear(); +builder.Configuration.AddIniFile("configuration.ini", false, true); +builder.Configuration.AddIniFile("configuration.overrides.ini", true, true); + builder.Services.AddControllers().AddNewtonsoftJson(); builder.Services.AddApiVersioning(options => { options.DefaultApiVersion = new ApiVersion(1); @@ -13,6 +17,7 @@ builder.Services.AddApiVersioning(options => { builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddRazorPages(); +//TODO: load built assets in production builder.Services.AddViteServices(options => { options.PackageDirectory = "../Iceshrimp.Frontend"; options.PackageManager = "yarn"; @@ -25,8 +30,6 @@ builder.Services.AddLogging(logging => logging.AddSimpleConsole(options => { })); builder.Services.AddDbContext(); -//TODO: load built assets in production - var app = builder.Build(); app.Logger.LogInformation("Initializing, please wait..."); diff --git a/Iceshrimp.Backend/appsettings.Development.json b/Iceshrimp.Backend/appsettings.Development.json deleted file mode 100644 index 0c208ae9..00000000 --- a/Iceshrimp.Backend/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/Iceshrimp.Backend/appsettings.json b/Iceshrimp.Backend/appsettings.json deleted file mode 100644 index 10f68b8c..00000000 --- a/Iceshrimp.Backend/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} diff --git a/Iceshrimp.Backend/configuration.ini b/Iceshrimp.Backend/configuration.ini new file mode 100644 index 00000000..becc482d --- /dev/null +++ b/Iceshrimp.Backend/configuration.ini @@ -0,0 +1,11 @@ +AllowedHosts=* + +[Logging:LogLevel] +Default=Information +Microsoft.AspNetCore=Warning + +[Database] +Host=localhost +Database=iceshrimp +Username=iceshrimp +Password=iceshrimp \ No newline at end of file