added database to store metadata

This commit is contained in:
2025-04-10 19:56:35 +05:00
parent c4af1f31e8
commit d7dcd7afc9
15 changed files with 278 additions and 153 deletions
+26 -39
View File
@@ -11,28 +11,25 @@ global using ParadoxSaveParser.Lib;
global using Directory = DTLib.Filesystem.Directory;
global using File = DTLib.Filesystem.File;
global using Path = DTLib.Filesystem.Path;
using System.Collections.Concurrent;
using System.IO;
using DTLib.Console;
using DTLib.Dtsod;
using DTLib.Web;
using DTLib.Web.Routes;
using ParadoxSaveParser.WebAPI.BackgroundTasks;
using ParadoxSaveParser.WebAPI.Database;
using ParadoxSaveParser.WebAPI.Routes;
using ParadoxSaveParser.WebAPI.SaveDataFilters;
// ReSharper disable MethodHasAsyncOverload
namespace ParadoxSaveParser.WebAPI;
public static class Program
{
private static readonly IOPath _configPath = "./config.dtsod";
private static Config _config = new();
private static bool IsDebug = true;
private static readonly CancellationTokenSource _mainCancel = new();
internal static readonly ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
internal static bool IsDebug = true;
internal static DatabaseConnector DB = null!;
public static void Main(string[] args)
public static async Task Main(string[] args)
{
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
@@ -59,45 +56,37 @@ public static class Program
});
var loggerMain = new ContextLogger(nameof(Main), loggerRoot);
loggerMain.LogDebug("Debug log is enabled");
CancellationTokenSource mainCancel = new();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
loggerMain.LogInfo("Ctrl+C Pressed");
_mainCancel.Cancel();
mainCancel.Cancel();
};
try
{
// config
if (!File.Exists(_configPath))
IOPath configPath = "./config.dtsod";
Config config;
if (File.Exists(configPath))
{
loggerMain.LogWarn("config file not found.");
File.WriteAllText(_configPath, _config.ToString());
loggerMain.LogWarn($"created default at {_configPath}.");
config = Config.FromDtsod(new DtsodV23(File.ReadAllText(configPath)));
}
else
{
_config = Config.FromDtsod(new DtsodV23(File.ReadAllText(_configPath)));
loggerMain.LogWarn("config file not found.");
config = new();
File.WriteAllText(configPath, config.ToString());
loggerMain.LogWarn($"created default at {configPath}.");
}
PathHelper.CreateProgramDirectories();
var metaFiles = System.IO.Directory.GetFiles(
PathHelper.SAVES_DIR.Str, "*.meta.json",
SearchOption.TopDirectoryOnly);
foreach (string metaFilePath in metaFiles)
{
using var metaFile = File.OpenRead(metaFilePath);
var meta = JsonSerializer.Deserialize<SaveFileMetadata>(metaFile) ??
throw new NullReferenceException(metaFilePath);
if (meta.status != SaveFileProcessingStatus.Done)
loggerMain.LogWarn(nameof(Main),
$"metadata file '{metaFilePath}' status has invalid status {meta.status}");
if (!_saveMetadataStorage.TryAdd(meta.id, meta))
throw new Exception("Guid collision!");
}
PathHelper.CleanTempDirectory();
DB = new("database.sqlite", 30, loggerRoot);
await DB.InitializeAsync();
await DB.DeleteInvalidData();
var bgJobManager = new BackgroundJobManager(loggerRoot);
var saveFilters = new Dictionary<Game, ISaveDataFilter>
@@ -108,13 +97,13 @@ public static class Program
// http server
var router = new SimpleRouter(loggerRoot);
router.DefaultRoute = new ServeFilesRouteHandler("public");
router.MapRoute("/getSaveStatus", HttpMethod.GET, new GetSaveStatusHandler(_mainCancel.Token));
router.MapRoute("/uploadSave/eu4", HttpMethod.POST, new UploadSaveHandler(_mainCancel.Token,
router.MapRoute("/getSaveStatus", HttpMethod.GET, new GetSaveStatusHandler(mainCancel.Token));
router.MapRoute("/uploadSave", HttpMethod.POST, new UploadSaveHandler(mainCancel.Token,
bgJobManager, saveFilters));
router.MapRoute("/getSaveData", HttpMethod.GET, new GetSaveDataHandler(_mainCancel.Token));
router.MapRoute("/getSaveData", HttpMethod.GET, new GetSaveDataHandler(mainCancel.Token));
var app = new WebApp(_config.BaseUrl, loggerRoot, router, _mainCancel.Token);
app.Run().GetAwaiter().GetResult();
var app = new WebApp(config.BaseUrl, loggerRoot, router, mainCancel.Token);
await app.Run();
}
catch (OperationCanceledException ex)
{
@@ -125,6 +114,4 @@ public static class Program
loggerMain.LogError(ex.ToStringDemystified());
}
}
}