From c4f1a1ca81aaee5d159872df8835e8c3261950c1 Mon Sep 17 00:00:00 2001 From: Elias Jansson Date: Sat, 24 Jan 2026 15:27:49 +0100 Subject: [PATCH] =?UTF-8?q?Nus=C3=A5=20kanske?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aberwyn/Data/SetupService.cs | 47 ++++++++------------------ Aberwyn/Data/infrastructure/setup.json | 11 ++++++ 2 files changed, 26 insertions(+), 32 deletions(-) create mode 100644 Aberwyn/Data/infrastructure/setup.json diff --git a/Aberwyn/Data/SetupService.cs b/Aberwyn/Data/SetupService.cs index 45063c2..6e72683 100644 --- a/Aberwyn/Data/SetupService.cs +++ b/Aberwyn/Data/SetupService.cs @@ -4,26 +4,25 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; using System.IO; +using System.Text.Json; +using Microsoft.Extensions.Hosting; namespace Aberwyn.Data { + // SetupService.cs public class SetupService { + private readonly IWebHostEnvironment _env; private readonly string _filePath; - public SetupService() + public SetupService(IWebHostEnvironment env) { - // Persistenta data ligger i /app/data + _env = env; var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data"); // /app/data i containern _filePath = Path.Combine(dataRoot, "infrastructure", "setup.json"); - - // Skapa mappen om den inte finns - Directory.CreateDirectory(Path.GetDirectoryName(_filePath)!); } - /// - /// Hämtar setup-inställningar - /// + public SetupSettings GetSetup() { if (!File.Exists(_filePath)) @@ -33,22 +32,12 @@ namespace Aberwyn.Data return JsonSerializer.Deserialize(json) ?? new SetupSettings { IsConfigured = false }; } - /// - /// Sparar setup-inställningar - /// - public void SaveSetup(SetupSettings setup) - { - var json = JsonSerializer.Serialize(setup, new JsonSerializerOptions { WriteIndented = true }); - File.WriteAllText(_filePath, json); - } - /// - /// Temporär service provider för initial setup / EF + Identity - /// internal static IServiceProvider BuildTemporaryServices(string connectionString) { var services = new ServiceCollection(); + // Konfigurera EF + Identity services.AddDbContext(options => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))); @@ -56,28 +45,20 @@ namespace Aberwyn.Data .AddEntityFrameworkStores() .AddDefaultTokenProviders(); + // Lägg till en tom konfiguration för att undvika null services.AddSingleton(new ConfigurationBuilder().Build()); + + // Valfritt: Lägg till loggning om något kräver det services.AddLogging(); return services.BuildServiceProvider(); } - /// - /// Loader-klass för enkel läsning utan instans - /// public static class SetupLoader { - public static SetupSettings Load() + public static SetupSettings Load(IHostEnvironment env) { - var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data"); - var path = Path.Combine(dataRoot, "infrastructure", "setup.json"); - - // Skapa mappen om den inte finns - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - - if (!File.Exists(path)) - return new SetupSettings { IsConfigured = false }; - + var path = Path.Combine(env.ContentRootPath, "infrastructure", "setup.json"); var json = File.ReadAllText(path); return JsonSerializer.Deserialize(json)!; } @@ -87,5 +68,7 @@ namespace Aberwyn.Data return $"server={setup.DbHost};port={setup.DbPort};database={setup.DbName};user={setup.DbUser};password={setup.DbPassword}"; } } + } + } diff --git a/Aberwyn/Data/infrastructure/setup.json b/Aberwyn/Data/infrastructure/setup.json new file mode 100644 index 0000000..eba5681 --- /dev/null +++ b/Aberwyn/Data/infrastructure/setup.json @@ -0,0 +1,11 @@ +{ + "AdminUsername": "admin", + "AdminEmail": "admin@localhost", + "AdminPassword": "Admin123!", + "IsConfigured": true, + "DbHost": "192.168.1.108", + "DbPort": 3306, + "DbName": "lewel_prod", + "DbUser": "lewel", + "DbPassword": "W542.Hl;)%ta" +} \ No newline at end of file