Decision
One image, one process, three names. The image carries TheYard and The Shed. The process answers theyard.stevenstout.biz, theyard-cosmos.stevenstout.biz and theshed.stevenstout.biz (the Hosts settings). Which name a request arrived on is its Host header when App Service routed it by one of the three names, which is how The Shed's domain arrives, bound straight to the web app; the two Yard domains arrive through the edge, which proxies both to the web app's own address and says which domain the visitor typed in X-Yard-Host, read only when the Host is not one of the three. A header counts only when it names one of the three configured names, so the most a sender can choose is which of the three sites answers.
public sealed class SiteHosts
{
public const string HeaderName = "X-Yard-Host";
private readonly Site[] _sites;
private readonly string? _siteUrl;
public SiteHosts(IEnumerable<Site> sites, string? siteUrl = null)
{
_siteUrl = string.IsNullOrWhiteSpace(siteUrl) || siteUrl.StartsWith("__", StringComparison.Ordinal) ? null : siteUrl.Trim().TrimEnd('/');
_sites = [.. sites
.Select(site => site with { Host = Clean(site.Host) ?? "", Store = string.IsNullOrWhiteSpace(site.Store) ? null : site.Store.Trim() })
.Where(site => site.Host.Length > 0)];
}
public static SiteHosts None { get; } = new([]);
public IReadOnlyList<Site> All => _sites;
public IEnumerable<Site> StoreSites => _sites.Where(site => site.Store is not null);
public IEnumerable<string> Guests => _sites.Where(site => site.Store is null).Select(site => site.Host);
public string? Of(HttpContext? context)
{
if (context is null)
{
return null;
}
if (Known(context.Request.Host.Host) is { } routed)
{
return routed.Host;
}
return context.Request.Headers.TryGetValue(HeaderName, out var said) ? Known(said.ToString())?.Host : null;
}
public string? StoreFor(string? host) => Array.Find(_sites, site => site.Host == host)?.Store;
public string? HostFor(string key) =>
Array.Find(_sites, site => string.Equals(site.Store, key, StringComparison.OrdinalIgnoreCase))?.Host;
public string? OriginOf(string key) => HostFor(key) is { } host ? $"https://{host}" : null;
public string? SiteOrigin(HttpContext? context) =>
Of(context) is { } host && StoreFor(host) is not null ? $"https://{host}" : null;
public string LinkOrigin(HttpContext context) =>
SiteOrigin(context) ?? _siteUrl ?? $"{context.Request.Scheme}://{context.Request.Host.Value}";
public string? GuestOf(HttpContext context) =>
Of(context) is { } host && StoreFor(host) is null ? host : null;
private Site? Known(string? host)
{
string? clean = Clean(host);
return clean is null ? null : Array.Find(_sites, site => site.Host == clean);
}
private static string? Clean(string? host) =>
string.IsNullOrWhiteSpace(host) || host.StartsWith("__", StringComparison.Ordinal)
? null
: host.Trim().TrimEnd('.').ToLowerInvariant();
}
public sealed record Site(string Host, string? Store);
Live from api/TheYard.Api/Shared/Instance/SiteHosts.cs, region site-hosts, read from this build at 99df7a8.
The store follows the name. A request that names no store gets the store of its site; the X-Yard-Store header still wins, which is how the proof and the measurements ask for a store. Store:Default is gone from the template: with no name at all (a health probe, the deploy's first read of the origin) the process answers with the relational store. Each store's catalogue is kept by this process, both of them, as the only process that keeps them (ADR: The store does the work). The other site's address on the store bar comes from the names, so the link from one site to the other still goes to the other domain.
public sealed class Backends
{
public const string LegacyCookieName = "yard-store";
public const string HeaderName = "X-Yard-Store";
private readonly Backend[] _all;
private readonly SiteHosts _hosts;
public Backends(IEnumerable<Backend> all, string? defaultKey, string? otherSite = null, SiteHosts? hosts = null)
{
_hosts = hosts ?? SiteHosts.None;
_all = all.ToArray();
if (_all.Length == 0)
{
throw new ArgumentException("a container needs at least one store", nameof(all));
}
Default = Named(defaultKey) ?? _all[0];
OtherSite = Uri.TryCreate(otherSite, UriKind.Absolute, out var site)
&& (site.Scheme == Uri.UriSchemeHttp || site.Scheme == Uri.UriSchemeHttps)
? site.GetLeftPart(UriPartial.Authority)
: null;
}
public IReadOnlyList<Backend> All => _all;
public string? OtherSite { get; }
public Backend Default { get; }
public Backend? Named(string? key) =>
key is null ? null : Array.Find(_all, backend => string.Equals(backend.Key, key, StringComparison.OrdinalIgnoreCase));
public Backend For(HttpContext? context)
{
if (context is null)
{
return Default;
}
if (context.Request.Headers.TryGetValue(HeaderName, out var header) && Named(header) is { } byHeader)
{
return byHeader;
}
return SiteDefault(context);
}
public Backend SiteDefault(HttpContext? context) =>
Named(_hosts.StoreFor(_hosts.Of(context))) ?? Default;
public string? OtherSiteFor(HttpContext? context)
{
var here = SiteDefault(context);
var other = _all.FirstOrDefault(backend => !ReferenceEquals(backend, here));
return other is not null && _hosts.OriginOf(other.Key) is { } origin ? origin : OtherSite;
}
public static void ExpireLegacyCookie(HttpContext context)
{
if (context.Request.Cookies.ContainsKey(LegacyCookieName))
{
context.Response.Cookies.Delete(LegacyCookieName, new CookieOptions { Path = "/" });
}
}
public StoresView Describe(HttpContext? context)
{
var current = For(context);
var siteDefault = SiteDefault(context);
return new StoresView(
current.Key,
_all.Select(backend => new StoreView(
backend.Key,
backend.Name,
backend.Ready,
ReferenceEquals(backend, siteDefault))).ToArray(),
OtherSiteFor(context));
}
}
public sealed class CurrentBackend(Backends backends, IHttpContextAccessor accessor)
{
public Backend Backend { get; } = backends.For(accessor.HttpContext);
public string Key => Backend.Key;
public string Name => Backend.Name;
public StandingReader Standings => new(Backend.Inventory, Backend.Bids, Backend.Market);
}
public static class Warmth
{
public static Task EnsureAsync(Backend backend, CancellationToken cancellation) =>
backend.Inventory.IsWarm ? Task.CompletedTask : backend.Inventory.WarmAsync(cancellation);
}
public sealed record StoreView(string Key, string Name, bool Ready, bool Default);
public sealed record StoresView(string Current, IReadOnlyList<StoreView> Stores, string? OtherSite);
Live from api/TheYard.Api/Shared/Stores/Stores.cs, region backends, read from this build at 99df7a8.
The Shed is its own application, started inside this process. The Shed's Program.cs stays the way a reader starts it on their own machine. ShedMount starts the same application in this process: The Shed's own builder, its own four registrations, its own pipeline and controllers, in a service container of its own, with a server that has no socket where Kestrel would be. A request on The Shed's name is handed to that application before anything of TheYard's pipeline sees it. Starting it whole, rather than registering its services on TheYard's builder, removes every collision between the two service containers by construction: its Kestrel body limit configures a server that never listens, and its upload limit is set on each of its own requests instead; its rate limiter, exception handler and snake_case JSON stay in its container; its /healthz is reached only on its name; its documents, sample home and live sources are read from its own published folder, /app/shed, which Shed__ContentRoot names. What the two still share is the process: one memory limit, one set of environment variables (each reads only its own settings), and the framework's process-wide telemetry sources, so Application Insights may see a Shed request at both layers. Its lifetime is TheYard's: it answers no signal of its own and is stopped when TheYard stops. A Shed that cannot start answers its name with a 503 and leaves the Yard names running; a failure past The Shed's own error handler is logged in TheYard's log.
public static class HostedSiteMount
{
public static async Task MountTheHostedSitesAsync(this WebApplication app, CancellationToken stopping)
{
var sites = app.Services.GetServices<IHostedSite>().ToList();
if (sites.Count == 0)
{
return;
}
var hosts = app.Services.GetRequiredService<SiteHosts>();
var errorLog = app.Services.GetRequiredService<ErrorRings>().Server;
foreach (var site in sites)
{
try
{
await site.StartAsync(stopping);
app.Logger.LogInformation("The hosted site {Host} is running", site.Host);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
app.Logger.LogError(ex, "The hosted site {Host} did not start ({Reason}); its name answers 503", site.Host, ex.GetType().Name);
}
}
app.Use(async (context, next) =>
{
if (hosts.GuestOf(context) is not { } guest || sites.Find(site => site.Host == guest) is not { } site)
{
await next();
return;
}
if (!site.Running)
{
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await context.Response.WriteAsync($"The site {site.Host} is not running in this process; the log says why.", context.RequestAborted);
return;
}
try
{
await site.HandleAsync(context, context.RequestAborted);
}
catch (Exception ex) when (!context.RequestAborted.IsCancellationRequested)
{
app.Logger.LogError(ex, "The hosted site {Host} failed a request to {Path}", site.Host, context.Request.Path);
errorLog.Record(context.Request.Path, StatusCodes.Status500InternalServerError, ex.GetType().Name, StackFrames.Of(ex));
if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
await context.Response.WriteAsJsonAsync(new { title = $"{site.Host} could not answer", status = 500 }, context.RequestAborted);
}
}
});
}
}
Live from api/TheYard.Api/Shared/Sites/HostedSiteMount.cs, region hosted-sites, read from this build at 99df7a8.
The peer card is retired with a sentence. It read the other web app's metrics; the other site is now this process, and the comparison card already sets the two stores side by side. With no Peer__Url, the card says exactly that (an addendum on ADR: Backends, side by side).
The renderer names the store. Both sites' pages are read from the one web app's address, so every read the renderer makes carries X-Yard-Store with the site it draws for. It stays a web app of its own: Node is its own runtime and its own roll (ADR: A rendering service beside the API).
The edge moves after the origin it names is verified. The two Yard domains' proxy rules moved from edge/_redirects to netlify.toml, the only place a rule can add a header to what it proxies. The Edge branch workflow now starts when Deploy succeeds and only then moves the edge, then reads both names until each serves the build; before, it moved on the push itself, which on this change would have sent visitors to a web app still starting.
Deploy rolls one web app. deploy.yml builds the image, rolls APP-THEYARD-ONE-SS and, before any domain, reads the Cosmos DB site's name and The Shed's name at the origin with X-Yard-Host: the first must get Azure Cosmos DB with no store named, the second The Shed's own health check and page. Deploy Cosmos and Deploy The Shed retired with their web apps, and so did the two container group files and The Shed's own template.