Server reale e veste Docker (M5): composizione, collegamento, vista remota, salute, immagine e modello Unraid
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
35 files changed
+6124
-10
No files matched your search
@@ -0,0 +1,114 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Febe.Server;
|
||||
|
||||
/// <summary>
|
||||
/// The real server (default when neither <c>--demo</c> nor <c>--simula</c> is given): the Docker edition with the
|
||||
/// browser, the engine and the password-protected interface. Stops cleanly on SIGTERM (<c>docker stop</c>) and Ctrl+C.
|
||||
/// Exit code 0 after a clean stop, 1 when the interface cannot start, 2 for a wrong command line.
|
||||
/// </summary>
|
||||
internal static class ServerCommand
|
||||
{
|
||||
public const string Usage =
|
||||
"Uso: febe [--dati /dati] [--libreria /libreria] [--importa /importa] [--porta 8480] [--ascolta 0.0.0.0]\n"
|
||||
+ " [--porta-debug 9222] [--display :99] [--chromium percorso] [--senza-sandbox]\n"
|
||||
+ " [--sito-di-prova http://127.0.0.1:8481 [--cdn-di-prova http://127.0.0.2:8482] [--accesso-di-prova]] [--prima-sessione secondi]\n"
|
||||
+ "Variabili: FEBE_PASSWORD (obbligatoria), FEBE_PORTA, TZ, LANG, FEBE_HOST, FEBE_PROXY_FIDATO, FEBE_SENZA_SANDBOX, FEBE_CHROMIUM.\n"
|
||||
+ "Altri comandi: --demo, --simula 30g --profilo prudente, --controllo-salute.";
|
||||
|
||||
public static async Task<int> RunAsync(string[] args, TextWriter output, TextWriter error)
|
||||
{
|
||||
if (!ServerOptions.TryParse(args, Environment.GetEnvironmentVariable, out var options, out var problem))
|
||||
{
|
||||
await error.WriteLineAsync(problem).ConfigureAwait(false);
|
||||
await error.WriteLineAsync(Usage).ConfigureAwait(false);
|
||||
return 2;
|
||||
}
|
||||
|
||||
using var stop = new CancellationTokenSource();
|
||||
ConsoleCancelEventHandler onCancel = (_, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
stop.Cancel();
|
||||
};
|
||||
Console.CancelKeyPress += onCancel;
|
||||
using var terminate = PosixSignalRegistration.Create(PosixSignal.SIGTERM, context =>
|
||||
{
|
||||
context.Cancel = true;
|
||||
stop.Cancel();
|
||||
});
|
||||
try
|
||||
{
|
||||
FebeServer server;
|
||||
try
|
||||
{
|
||||
server = await FebeServer.StartAsync(options, new ServerHooks { Console = output }, stop.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (IOException ex) when (ex.Message.Contains("address", StringComparison.OrdinalIgnoreCase) || ex.InnerException is System.Net.Sockets.SocketException)
|
||||
{
|
||||
await error.WriteLineAsync($"La porta {options.Port} è già occupata: cambia FEBE_PORTA o ferma l'altro programma.").ConfigureAwait(false);
|
||||
return 1;
|
||||
}
|
||||
catch (OperationCanceledException) when (stop.IsCancellationRequested)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
await using (server.ConfigureAwait(false))
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(Timeout.Infinite, stop.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.CancelKeyPress -= onCancel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <c>--controllo-salute</c>: the Docker HEALTHCHECK. Asks <c>/salute</c> on 127.0.0.1 and exits 0 for «ok», 1 otherwise.
|
||||
/// The image has no curl: the program checks itself.
|
||||
/// </summary>
|
||||
internal static class HealthCheckCommand
|
||||
{
|
||||
public const string Flag = "--controllo-salute";
|
||||
|
||||
public static bool Matches(string[] args) => args.Any(a => string.Equals(a, Flag, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
public static async Task<int> RunAsync(Func<string, string?> environment, TextWriter output)
|
||||
{
|
||||
var port = int.TryParse(environment("FEBE_PORTA"), System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var p) && p is >= 1 and <= 65535
|
||||
? p
|
||||
: Febe.Web.FebeListen.DefaultPort;
|
||||
return await CheckAsync(new Uri($"http://127.0.0.1:{port}/salute"), output).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static async Task<int> CheckAsync(Uri address, TextWriter output)
|
||||
{
|
||||
using var client = new HttpClient(new SocketsHttpHandler { UseProxy = false, UseCookies = false, AllowAutoRedirect = false })
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(8),
|
||||
};
|
||||
try
|
||||
{
|
||||
using var response = await client.GetAsync(address).ConfigureAwait(false);
|
||||
var body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
await output.WriteLineAsync(body).ConfigureAwait(false);
|
||||
return response.IsSuccessStatusCode ? 0 : 1;
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException)
|
||||
{
|
||||
await output.WriteLineAsync("Febe non risponde.").ConfigureAwait(false);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user