This commit is contained in:
@@ -4,26 +4,25 @@ 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()
|
public SetupService(IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
// Persistenta data ligger i /app/data
|
_env = env;
|
||||||
var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data"); // /app/data i containern
|
var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data"); // /app/data i containern
|
||||||
_filePath = Path.Combine(dataRoot, "infrastructure", "setup.json");
|
_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))
|
||||||
@@ -33,22 +32,12 @@ 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)));
|
||||||
|
|
||||||
@@ -56,28 +45,20 @@ 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()
|
public static SetupSettings Load(IHostEnvironment env)
|
||||||
{
|
{
|
||||||
var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data");
|
var path = Path.Combine(env.ContentRootPath, "infrastructure", "setup.json");
|
||||||
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)!;
|
||||||
}
|
}
|
||||||
@@ -87,5 +68,7 @@ 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}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
11
Aberwyn/Data/infrastructure/setup.json
Normal file
11
Aberwyn/Data/infrastructure/setup.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user