using Febe.Core.Abstractions;
using Febe.Core.Engine;
using Febe.Core.Export;
using Febe.Core.Import;
using Febe.Core.Library;
using Febe.Core.Storage;
using Febe.Data;
using Febe.Web.Connection;
using Febe.Web.Engine;
using Febe.Web.Library;
using Febe.Web.Native;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace Febe.Web.Demo;
/// Options of --demo.
/// Port on 127.0.0.1 (8480 by default; 0 picks a free one).
/// Alberto's time zone for the demo (the system zone by default).
/// Parent of the temporary folder (the system temporary folder by default).
/// Development: serve the interface from this folder (see ).
public sealed record DemoOptions(int Port = FebeListen.DefaultPort, TimeZoneInfo? Zone = null, string? Root = null, string? InterfaceFolder = null);
///
/// Demo mode (spec ยง10.6): starts without browser and network, listening only on 127.0.0.1 and without password, with
/// a temporary Data folder and a temporary library seeded with coherent demo data. It cannot write to the real
/// library: every path lives under a new temporary folder, removed when the demo ends.
///
public sealed class DemoHost : IAsyncDisposable
{
private readonly FebeDatabase _database;
private readonly ImportService _import;
private DemoHost(string root, FebeDatabase database, ImportService import, WebApplication app, DemoWorld world, Uri address)
{
Root = root;
_database = database;
_import = import;
App = app;
World = world;
Address = address;
}
/// The temporary folder of this demo (Data and library inside).
public string Root { get; }
public WebApplication App { get; }
public DemoWorld World { get; }
/// Where the interface answers, e.g. http://127.0.0.1:8480/.
public Uri Address { get; }
public IFebeStorage Storage => _database;
public static async Task StartAsync(DemoOptions options, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(options);
var root = Path.Combine(options.Root ?? Path.GetTempPath(), "febe-dimostrazione-" + Guid.NewGuid().ToString("N")[..10]);
Directory.CreateDirectory(root);
FebeDatabase? database = null;
ImportService? import = null;
try
{
var clock = new DemoClock(options.Zone ?? TimeZoneInfo.Local, DateTimeOffset.UtcNow);
var data = new DataFolderLayout(Path.Combine(root, "dati"));
var layout = new LibraryLayout(Path.Combine(root, "libreria"));
Directory.CreateDirectory(data.Root);
Directory.CreateDirectory(layout.Root);
database = await FebeDatabase.OpenAsync(data.Root, ct).ConfigureAwait(false);
await database.Settings.SetAsync(SettingKeys.LibraryRoot, layout.Root, ct).ConfigureAwait(false);
var world = await DemoSeeder.SeedAsync(database, data, layout, clock, ct).ConfigureAwait(false);
import = await DemoImport.PrepareAsync(root, database, world, layout, data, clock, ct).ConfigureAwait(false);
await DemoExport.SeedAsync(root, database, world, layout, data, clock, ct).ConfigureAwait(false);
var exportRoot = DemoExport.Root(root);
var demoImport = import;
var library = new FebeLibrary(database, data, layout, clock);
var web = new FebeWebOptions
{
Edition = FebeEdition.Desktop,
Access = AccessMode.None,
AllowedHosts = FebeWebOptions.LoopbackHosts(options.Port),
Demo = true,
InterfaceFolder = options.InterfaceFolder,
};
var db = database;
var app = FebeWebHost.Build(web, FebeListen.Loopback(options.Port), services =>
{
services.AddSingleton(db);
services.AddSingleton(library);
services.AddSingleton(clock);
services.AddSingleton(world);
services.AddSingleton();
services.AddSingleton(sp => sp.GetRequiredService());
services.AddSingleton(sp => sp.GetRequiredService());
services.AddSingleton(sp => new DemoProfileSearch(db, clock, sp.GetRequiredService()));
services.AddSingleton(sp => new DemoShell(sp.GetRequiredService(), root));
services.AddSingleton(sp => sp.GetRequiredService());
services.AddSingleton(sp => sp.GetRequiredService());
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(demoImport);
services.AddSingleton(exportRoot);
services.AddSingleton();
services.AddSingleton(NoEngineSession.Instance);
});
await app.StartAsync(ct).ConfigureAwait(false);
return new DemoHost(root, database, import, app, world, FebeWebHost.BoundAddress(app));
}
catch
{
if (import is not null)
{
await import.DisposeAsync().ConfigureAwait(false);
}
if (database is not null)
{
await database.DisposeAsync().ConfigureAwait(false);
}
TryDelete(root);
throw;
}
}
public async ValueTask DisposeAsync()
{
try
{
await App.StopAsync().ConfigureAwait(false);
}
finally
{
await App.DisposeAsync().ConfigureAwait(false);
await _import.DisposeAsync().ConfigureAwait(false);
await _database.DisposeAsync().ConfigureAwait(false);
TryDelete(Root);
}
}
private static void TryDelete(string root)
{
try
{
Directory.Delete(root, recursive: true);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
}