feat(server): web server, interface shell, demo mode and Docker image (refs #11 #12 #13 #14 #15 #16 #17)
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>
This commit is contained in:
1 parent
53ca01b7c3
commit
f9d71559e8
71 files changed
+5853
-80
No files matched your search
@@ -0,0 +1,138 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Anubis.Core;
|
||||
|
||||
namespace Anubis.Server;
|
||||
|
||||
/// <summary>
|
||||
/// Structured logs (SPEC 2.2, issue #10): one JSON object per line on the console and in a daily file under
|
||||
/// data/logs, every text passed through the <see cref="Redactor"/> so no secret reaches a log. The last 14 files are kept.
|
||||
/// </summary>
|
||||
internal sealed class JsonLogProvider : ILoggerProvider
|
||||
{
|
||||
public const int KeepFiles = 14;
|
||||
|
||||
private readonly string? _folder;
|
||||
private readonly IClock _clock;
|
||||
private readonly bool _console;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly ConcurrentDictionary<string, JsonLogger> _loggers = new(StringComparer.Ordinal);
|
||||
private StreamWriter? _file;
|
||||
private string? _fileDay;
|
||||
|
||||
public JsonLogProvider(string? folder, IClock clock, bool console)
|
||||
{
|
||||
_folder = folder;
|
||||
_clock = clock;
|
||||
_console = console;
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new JsonLogger(name, this));
|
||||
|
||||
internal void Write(string category, LogLevel level, EventId eventId, string message, Exception? exception)
|
||||
{
|
||||
var now = _clock.UtcNow;
|
||||
using var buffer = new MemoryStream();
|
||||
using (var json = new Utf8JsonWriter(buffer))
|
||||
{
|
||||
json.WriteStartObject();
|
||||
json.WriteString("at", now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture));
|
||||
json.WriteString("level", level.ToString().ToLowerInvariant());
|
||||
json.WriteString("category", category);
|
||||
if (eventId.Id != 0)
|
||||
{
|
||||
json.WriteNumber("event", eventId.Id);
|
||||
}
|
||||
|
||||
json.WriteString("message", Redactor.Redact(message));
|
||||
if (exception is not null)
|
||||
{
|
||||
json.WriteString("error", Redactor.Redact(exception.GetType().Name + ": " + exception.Message));
|
||||
json.WriteString("stack", Redactor.Redact(exception.StackTrace));
|
||||
}
|
||||
|
||||
json.WriteEndObject();
|
||||
}
|
||||
|
||||
var line = Encoding.UTF8.GetString(buffer.ToArray());
|
||||
lock (_gate)
|
||||
{
|
||||
if (_console)
|
||||
{
|
||||
Console.Out.WriteLine(line);
|
||||
}
|
||||
|
||||
WriteFile(now, line);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteFile(DateTimeOffset now, string line)
|
||||
{
|
||||
if (_folder is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var day = now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
|
||||
if (_file is null || _fileDay != day)
|
||||
{
|
||||
_file?.Dispose();
|
||||
Directory.CreateDirectory(_folder);
|
||||
_file = new StreamWriter(new FileStream(Path.Combine(_folder, "anubis-" + day + ".log"), FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete), new UTF8Encoding(false))
|
||||
{
|
||||
AutoFlush = true,
|
||||
};
|
||||
_fileDay = day;
|
||||
Prune();
|
||||
}
|
||||
|
||||
_file.WriteLine(line);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// A full or read-only disk must not stop the application: the console still has the line.
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private void Prune()
|
||||
{
|
||||
foreach (var old in Directory.EnumerateFiles(_folder!, "anubis-*.log").OrderDescending(StringComparer.Ordinal).Skip(KeepFiles))
|
||||
{
|
||||
File.Delete(old);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_file?.Dispose();
|
||||
_file = null;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class JsonLogger(string category, JsonLogProvider provider) : ILogger
|
||||
{
|
||||
public IDisposable? BeginScope<TState>(TState state)
|
||||
where TState : notnull => null;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
provider.Write(category, logLevel, eventId, formatter(state, exception), exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user