Split remaining service routes

This commit is contained in:
Alexa
2026-06-15 20:21:05 +03:00
parent bd4ceada65
commit 44f53e80b8
51 changed files with 2271 additions and 122 deletions
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace DorkNet.ServiceDefaults;
@@ -21,9 +22,15 @@ public static class DorkNetServiceDefaultsExtensions
return builder;
}
public static WebApplication MapDorkNetServiceDefaults(this WebApplication app)
public static WebApplication MapDorkNetServiceDefaults(
this WebApplication app,
bool mapPublicHealth = true)
{
app.MapHealthChecks("/healthz");
if (mapPublicHealth)
{
app.MapHealthChecks("/healthz");
}
app.MapGet("/internal/healthz", (DorkNetServiceInfo info) =>
{
return Results.Ok(new ServiceHealthResponse(
@@ -34,4 +41,43 @@ public static class DorkNetServiceDefaultsExtensions
return app;
}
public static WebApplication UseDorkNetRouteOwnershipGuard(
this WebApplication app,
string serviceName)
{
app.Use(async (ctx, next) =>
{
var domain = Environment.GetEnvironmentVariable("DORKNET_DOMAIN")
?? ctx.RequestServices.GetService<Microsoft.Extensions.Configuration.IConfiguration>()?["Domain:Apex"]
?? "localhost";
var path = ctx.Request.Path.Value ?? "/";
if (DorkNetRouteOwnership.IsOwnedBy(serviceName, ctx.Request.Host.Host, path, domain))
{
await next();
return;
}
var owner = DorkNetRouteOwnership.ResolvePublicService(ctx.Request.Host.Host, path, domain);
var logger = ctx.RequestServices
.GetRequiredService<ILoggerFactory>()
.CreateLogger("DorkNet.ServiceDefaults.RouteGuard");
logger.LogDebug(
"[route-guard] service={Service} rejected {Method} {Host}{Path}; owner={Owner}",
serviceName,
ctx.Request.Method,
ctx.Request.Host,
ctx.Request.Path,
owner);
ctx.Response.StatusCode = StatusCodes.Status404NotFound;
await ctx.Response.WriteAsJsonAsync(new
{
error = "route_not_owned_by_service",
service = serviceName,
owner,
});
});
return app;
}
}