swagger + agents

This commit is contained in:
JiriUhlir
2026-06-15 10:05:15 +02:00
parent cadfdaef1b
commit 1a898b95f9
3 changed files with 413 additions and 9 deletions
+16 -9
View File
@@ -79,26 +79,33 @@ builder.Services.AddSwaggerGen(options =>
var app = builder.Build();
if (!string.IsNullOrWhiteSpace(settings.RootPath))
// ROOT_PATH carries the public reverse-proxy prefix (e.g. /apps/idoklad). AppFactory's Caddy uses
// handle_path, which strips that prefix before the request reaches the container, so PathBase ends
// up empty and the OpenAPI server URL must come from ROOT_PATH directly (see the filter below).
var publicPrefix = string.IsNullOrWhiteSpace(settings.RootPath) ? null : "/" + settings.RootPath.Trim('/');
// UsePathBase is a no-op under handle_path (the request no longer carries the prefix) but keeps the
// app correct behind a proxy that forwards the prefix intact, so routes still resolve in that case.
if (publicPrefix is not null)
{
// UsePathBase requires a leading slash; tolerate ROOT_PATH configured without one.
var basePath = settings.RootPath.StartsWith('/') ? settings.RootPath : "/" + settings.RootPath;
app.UsePathBase(basePath);
app.UsePathBase(publicPrefix);
}
app.UseMiddleware<ExceptionHandlingMiddleware>();
// Serve the OpenAPI document under the same /docs prefix as the UI so a relative endpoint
// resolves correctly both locally and behind a reverse-proxy path base (ROOT_PATH).
// resolves correctly both locally and behind a reverse-proxy prefix (ROOT_PATH).
app.UseSwagger(options =>
{
options.RouteTemplate = "docs/{documentName}/swagger.json";
// Advertise the request path base (e.g. /apps/idoklad behind the portal proxy) as the
// OpenAPI server so Swagger UI "Try it out" targets {pathBase}/contacts, not the host root.
// Advertise the public prefix (e.g. /apps/idoklad behind the portal proxy) as the OpenAPI
// server so Swagger UI "Try it out" targets {prefix}/contacts, not the host root. The prefix
// comes from ROOT_PATH because handle_path has already stripped it from the request, leaving
// httpReq.PathBase empty. Fall back to PathBase (a proxy that keeps the prefix) and finally "/".
options.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
{
var basePath = httpReq.PathBase.HasValue ? httpReq.PathBase.Value : "/";
swaggerDoc.Servers = new List<OpenApiServer> { new() { Url = basePath } };
var serverUrl = publicPrefix ?? (httpReq.PathBase.HasValue ? httpReq.PathBase.Value : "/");
swaggerDoc.Servers = new List<OpenApiServer> { new() { Url = serverUrl } };
});
});
app.UseSwaggerUI(options =>