This commit is contained in:
@@ -4,24 +4,26 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
|
|
||||||
namespace Aberwyn.Data
|
namespace Aberwyn.Data
|
||||||
{
|
{
|
||||||
// SetupService.cs
|
|
||||||
public class SetupService
|
public class SetupService
|
||||||
{
|
{
|
||||||
private readonly IWebHostEnvironment _env;
|
|
||||||
private readonly string _filePath;
|
private readonly string _filePath;
|
||||||
|
|
||||||
public SetupService(IWebHostEnvironment env)
|
public SetupService()
|
||||||
{
|
{
|
||||||
_env = env;
|
// Persistenta data ligger i /app/data
|
||||||
_filePath = Path.Combine(_env.ContentRootPath, "infrastructure", "setup.json");
|
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()
|
public SetupSettings GetSetup()
|
||||||
{
|
{
|
||||||
if (!File.Exists(_filePath))
|
if (!File.Exists(_filePath))
|
||||||
@@ -31,12 +33,22 @@ namespace Aberwyn.Data
|
|||||||
return JsonSerializer.Deserialize<SetupSettings>(json) ?? new SetupSettings { IsConfigured = false };
|
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)
|
internal static IServiceProvider BuildTemporaryServices(string connectionString)
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
|
|
||||||
// Konfigurera EF + Identity
|
|
||||||
services.AddDbContext<ApplicationDbContext>(options =>
|
services.AddDbContext<ApplicationDbContext>(options =>
|
||||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
||||||
|
|
||||||
@@ -44,20 +56,28 @@ namespace Aberwyn.Data
|
|||||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|
||||||
// Lägg till en tom konfiguration för att undvika null
|
|
||||||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
|
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
|
||||||
|
|
||||||
// Valfritt: Lägg till loggning om något kräver det
|
|
||||||
services.AddLogging();
|
services.AddLogging();
|
||||||
|
|
||||||
return services.BuildServiceProvider();
|
return services.BuildServiceProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loader-klass för enkel läsning utan instans
|
||||||
|
/// </summary>
|
||||||
public static class SetupLoader
|
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);
|
var json = File.ReadAllText(path);
|
||||||
return JsonSerializer.Deserialize<SetupSettings>(json)!;
|
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}";
|
return $"server={setup.DbHost};port={setup.DbPort};database={setup.DbName};user={setup.DbUser};password={setup.DbPassword}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -340,7 +340,7 @@ function updateCriteria(id, isValid) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function submitSetup() {
|
async function submitSetup() {
|
||||||
goToStep(3);
|
goToStep(3);
|
||||||
const form = document.getElementById('setup-form');
|
const form = document.getElementById('setup-form');
|
||||||
const formData = new FormData(form);
|
const formData = new FormData(form);
|
||||||
|
|||||||
Reference in New Issue
Block a user