This commit is contained in:
@@ -4,24 +4,26 @@ 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(IWebHostEnvironment env)
|
||||
public SetupService()
|
||||
{
|
||||
_env = env;
|
||||
_filePath = Path.Combine(_env.ContentRootPath, "infrastructure", "setup.json");
|
||||
// Persistenta data ligger i /app/data
|
||||
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)!);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Hämtar setup-inställningar
|
||||
/// </summary>
|
||||
public SetupSettings GetSetup()
|
||||
{
|
||||
if (!File.Exists(_filePath))
|
||||
@@ -31,12 +33,22 @@ namespace Aberwyn.Data
|
||||
return JsonSerializer.Deserialize<SetupSettings>(json) ?? new SetupSettings { IsConfigured = false };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sparar setup-inställningar
|
||||
/// </summary>
|
||||
public void SaveSetup(SetupSettings setup)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(setup, new JsonSerializerOptions { WriteIndented = true });
|
||||
File.WriteAllText(_filePath, json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Temporär service provider för initial setup / EF + Identity
|
||||
/// </summary>
|
||||
internal static IServiceProvider BuildTemporaryServices(string connectionString)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
// Konfigurera EF + Identity
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
||||
|
||||
@@ -44,20 +56,28 @@ namespace Aberwyn.Data
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
// Lägg till en tom konfiguration för att undvika null
|
||||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
|
||||
|
||||
// Valfritt: Lägg till loggning om något kräver det
|
||||
services.AddLogging();
|
||||
|
||||
return services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loader-klass för enkel läsning utan instans
|
||||
/// </summary>
|
||||
public static class SetupLoader
|
||||
{
|
||||
public static SetupSettings Load(IHostEnvironment env)
|
||||
public static SetupSettings Load()
|
||||
{
|
||||
var path = Path.Combine(env.ContentRootPath, "infrastructure", "setup.json");
|
||||
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 json = File.ReadAllText(path);
|
||||
return JsonSerializer.Deserialize<SetupSettings>(json)!;
|
||||
}
|
||||
@@ -67,7 +87,5 @@ namespace Aberwyn.Data
|
||||
return $"server={setup.DbHost};port={setup.DbPort};database={setup.DbName};user={setup.DbUser};password={setup.DbPassword}";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user