using Febe.Core.Library; using Febe.Web.Connection; namespace Febe.Server; /// /// The checks made before anything starts (spec §8.2, §11): the interface password and the Data folder. With a problem /// the engine and the browser do not start, and the interface shows only the page that explains it. /// internal static class StartupChecks { public const string PasswordMissing = "password-mancante"; public const string DataNotWritable = "dati-non-scrivibili"; public const string DataOnNetwork = "dati-in-rete"; /// Every problem found, in the order the error page shows them. public static IReadOnlyList Run(ServerOptions options, DataFolderCheck? check = null) { ArgumentNullException.ThrowIfNull(options); var problems = new List(); if (string.IsNullOrEmpty(options.Password)) { problems.Add(new ConfigurationProblem( PasswordMissing, "Manca la password dell'interfaccia", "Il contenitore è partito senza la variabile FEBE_PASSWORD. Senza password l'interfaccia, e con lei la vista " + "remota del browser, sarebbe aperta a chiunque raggiunga il server: per questo Febe non ha avviato né il motore né il browser.", "Imposta FEBE_PASSWORD (nel modello di Unraid o in compose.yml) con una frase lunga che usi solo qui, poi riavvia il contenitore.")); } if ((check ?? new DataFolderCheck()).Check(options.DataFolder) is { } data) { problems.Add(new ConfigurationProblem( data.Kind == DataFolderProblemKind.NetworkPath ? DataOnNetwork : DataNotWritable, data.Title, data.Explanation, data.Remedy)); } return problems; } } /// The problems found at start-up; fixed for the life of the process (fixing one needs a restart). internal sealed class StartupProblems(IReadOnlyList problems) : IConfigurationProblems { public IReadOnlyList Current { get; } = problems; } /// /// The link to Instagram while a configuration problem keeps the browser from starting: always «browser non /// disponibile», and every command refused with the problem (the configuration gate refuses them earlier anyway). /// internal sealed class UnavailableConnectionService(IReadOnlyList problems) : IConnectionService { public Task GetStatusAsync(CancellationToken ct) => Task.FromResult(new ConnectionStatus(ConnectionState.BrowserUnavailable, null, null, false, null)); public Task BeginUserDrivingAsync(bool openLogin, CancellationToken ct) => throw Refused(); public Task FinishAsync(CancellationToken ct) => throw Refused(); public Task CancelAsync(CancellationToken ct) => Task.CompletedTask; public Task DisconnectAsync(CancellationToken ct) => throw Refused(); private Febe.Web.Api.BadRequestException Refused() => new($"{(problems.Count > 0 ? problems[0].Title : "Configurazione da sistemare")}: il browser di Febe non è partito."); }