Refactor WebGateway state classes and tuple usage

This commit is contained in:
Jacob Schmidt 2026-02-27 19:51:31 -06:00
parent 0dde7c40f3
commit 29e027b918

View File

@ -67,11 +67,11 @@ app.MapPost("/api/command", async (CommandEnvelope? command, Entry entry) =>
app.MapGet("/api/sidecar/root", (SidecarRootState rootState) => app.MapGet("/api/sidecar/root", (SidecarRootState rootState) =>
{ {
var snapshot = rootState.Get(); var (root, isCustom) = rootState.Get();
return Results.Ok(new return Results.Ok(new
{ {
root = snapshot.Root, root,
isCustom = snapshot.IsCustom isCustom
}); });
}); });
@ -84,12 +84,12 @@ app.MapPost("/api/sidecar/root", (SetSidecarRootRequest? request, SidecarRootSta
} }
rootState.Set(path); rootState.Set(path);
var snapshot = rootState.Get(); var (root, isCustom) = rootState.Get();
Environment.SetEnvironmentVariable("JOURNAL_PROJECT_ROOT", snapshot.Root); Environment.SetEnvironmentVariable("JOURNAL_PROJECT_ROOT", root);
return Results.Ok(new return Results.Ok(new
{ {
root = snapshot.Root, root,
isCustom = snapshot.IsCustom isCustom
}); });
}); });
@ -207,32 +207,20 @@ string ResolveWebDist(string repoRootPath)
string ErrorResponse(string message) string ErrorResponse(string message)
=> JsonSerializer.Serialize(new { ok = false, error = message }, gatewayJsonOptions); => JsonSerializer.Serialize(new { ok = false, error = message }, gatewayJsonOptions);
sealed class WebUiState sealed class WebUiState(string distPath)
{ {
public WebUiState(string distPath) public string DistPath { get; } = distPath;
{
DistPath = distPath;
}
public string DistPath { get; }
public bool Exists => Directory.Exists(DistPath) && File.Exists(Path.Combine(DistPath, "index.html")); public bool Exists => Directory.Exists(DistPath) && File.Exists(Path.Combine(DistPath, "index.html"));
} }
sealed class SidecarRootState sealed class SidecarRootState(string autoRoot)
{ {
private readonly object _sync = new(); private readonly object _sync = new();
private readonly string _autoRoot; private readonly string _autoRoot = autoRoot;
private string _currentRoot; private string _currentRoot = autoRoot;
private bool _isCustom; private bool _isCustom;
public SidecarRootState(string autoRoot)
{
_autoRoot = autoRoot;
_currentRoot = autoRoot;
_isCustom = false;
}
public (string Root, bool IsCustom) Get() public (string Root, bool IsCustom) Get()
{ {
lock (_sync) lock (_sync)
@ -272,5 +260,3 @@ sealed class CommandEnvelope
public string? Tag { get; set; } public string? Tag { get; set; }
public JsonElement? Payload { get; set; } public JsonElement? Payload { get; set; }
} }