Phase 1 completed on top of the library foundations: - Server: startup state machine (config, first run, database down, schema, identity, ready) with an error page that retries by itself; the configured database is the only one ever opened, and a vanished SQLite file is never recreated. REST API and status WebSocket; session cookie auth, CSRF on every form and write call; desktop and demo answer only the computer itself. - Interface: static Blazor rendering with plain forms (no interactive runtime), menu, top bar and phone bottom bar as in the mockups, light and dark themes with appendix C tokens and a contrast test, Panoramica, Diario, Rischio, Impostazioni; service pages for sign-in, first access, desktop first run and the database error page with its choices. - Demo mode (tasks seed-demo): appendix F in a database of its own under the temp folder, frozen clock, never touches the configured installation. - Docker: entrypoint gives /data to PUID:PGID and drops root with setpriv, HEALTHCHECK through bash /dev/tcp; proven with SQLite, PostgreSQL, a wrong configuration and a database that goes away and comes back. - tasks screenshots drives the installed Edge/Chrome through DevTools for exact 1440 and 390 pixel shots; anubis db export|import on the command line. Fixes from the independent review: return paths without control characters, Host check against DNS rebinding, attempt limit counted before the check and also on the error-page password, one-use TOTP codes and a code to switch the second factor off, error-page choices serialized with the startup loop, identity checked before migrating, restore restarts with "Ferma tutto" on, damaged archives reported instead of crashing. Running the server tests on PostgreSQL exposed pages rendered before their data arrived; data-loading components now load first (LoadedComponent). Decisions in ADR 0007. Tests: 100 on SQLite and on PostgreSQL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.7 KiB
C#
106 lines
3.7 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using Anubis.Core;
|
|
using Anubis.Demo;
|
|
using Anubis.Platform;
|
|
|
|
namespace Anubis.Server;
|
|
|
|
/// <summary>What the top bar shows and the WebSocket sends: state of the bot, mode, currency, how fresh the data are.</summary>
|
|
internal sealed record ShellSnapshot(
|
|
string Mode,
|
|
string Bot,
|
|
string BotLabel,
|
|
string BotShort,
|
|
bool RealMoney,
|
|
string Currency,
|
|
string Theme,
|
|
string Now,
|
|
int AgeSeconds,
|
|
bool Demo,
|
|
string Version);
|
|
|
|
/// <summary>Reads the shell state once per request; also the money formatter with the chosen display currency.</summary>
|
|
internal sealed class ShellData(AnubisHost host, IClock clock)
|
|
{
|
|
private decimal? _demoRate;
|
|
private int? _demoAge;
|
|
|
|
public bool IsDemo => host.IsDemo;
|
|
|
|
public TimeZoneInfo Zone => host.Config.TimeZone;
|
|
|
|
public DateTimeOffset Now => clock.UtcNow;
|
|
|
|
public async Task<ShellSnapshot> SnapshotAsync(CancellationToken cancellationToken)
|
|
{
|
|
var services = host.Services;
|
|
var bot = "active";
|
|
var currency = "EUR";
|
|
var theme = "auto";
|
|
if (services is not null)
|
|
{
|
|
bot = (await services.Settings.GetAsync(Core.SettingsStore.BotState.Key, cancellationToken).ConfigureAwait(false)).Value;
|
|
currency = (await services.Settings.GetAsync(Core.SettingsStore.Currency.Key, cancellationToken).ConfigureAwait(false)).Value;
|
|
theme = (await services.Settings.GetAsync(Core.SettingsStore.Theme.Key, cancellationToken).ConfigureAwait(false)).Value;
|
|
}
|
|
|
|
var (label, shortLabel) = bot switch
|
|
{
|
|
"paused" => ("Bot in pausa", "In pausa"),
|
|
"stopped" => ("Bot fermo", "Fermo"),
|
|
_ => ("Bot attivo", "Attivo"),
|
|
};
|
|
var age = DemoAge() ?? (host.LastCheckUtc is { } last ? (int)Math.Max(0, (clock.UtcNow - last).TotalSeconds) : 0);
|
|
return new ShellSnapshot(host.Status.Mode.ToString(), bot, label, shortLabel, RealMoney: false, currency, theme,
|
|
Format.LongDateTime(clock.UtcNow, Zone), age, host.IsDemo, AnubisInfo.Version);
|
|
}
|
|
|
|
/// <summary>
|
|
/// An amount in euro shown in the display currency. Exchange rates arrive with the market data (phase 2): until
|
|
/// then dollars appear only in demo mode, with the rate of appendix F, and otherwise amounts stay in euro.
|
|
/// </summary>
|
|
public string Money(decimal euro, string currency, bool withSign = false, int decimals = 2) =>
|
|
Format.Money(Convert(euro, currency), Effective(currency), withSign, decimals);
|
|
|
|
public decimal Convert(decimal euro, string currency) => currency == "USD" && DemoRate() is { } rate ? euro * rate : euro;
|
|
|
|
/// <summary>The display currency that <see cref="Money"/> can honour.</summary>
|
|
public string Effective(string currency) => currency == "USD" && DemoRate() is not null ? "USD" : "EUR";
|
|
|
|
private decimal? DemoRate()
|
|
{
|
|
if (!host.IsDemo)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (_demoRate is null && Aggregates() is { } doc)
|
|
{
|
|
_demoRate = decimal.Parse(doc.RootElement.GetProperty("cambio_eur_usd").GetString()!, CultureInfo.InvariantCulture);
|
|
}
|
|
|
|
return _demoRate;
|
|
}
|
|
|
|
private int? DemoAge()
|
|
{
|
|
if (!host.IsDemo)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (_demoAge is null && Aggregates() is { } doc)
|
|
{
|
|
_demoAge = doc.RootElement.GetProperty("dati_aggiornati_secondi").GetInt32();
|
|
}
|
|
|
|
return _demoAge;
|
|
}
|
|
|
|
/// <summary>The aggregates of appendix F; only in demo mode (SPEC 13.1).</summary>
|
|
public JsonDocument? Aggregates() => host.IsDemo ? DemoMode.ReadAggregates(host.Config.Data) : null;
|
|
|
|
public PlatformKind Platform => host.Config.Platform;
|
|
}
|