using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; namespace GB5Shared.Middleware; public static class GatewayPrefixMiddlewareExtensions { /// /// When a Host is reached through the GB5Build gateway under a module alias (e.g. /crm/, /prs/), /// the gateway strips that prefix before forwarding but sends it back via the X-GB5-Gateway-Prefix /// header. Setting it as PathBase here lets the app still generate correct self-referencing URLs /// (e.g. Swagger's swagger.json link) — this affects only URL *generation*, not endpoint routing /// (FastEndpoints matches on Request.Path, which is untouched), so real API calls are unaffected. /// Direct port access has no such header, so PathBase stays empty and behavior is unchanged. /// Note: the standard "X-Forwarded-Prefix" name doesn't work here — YARP treats the X-Forwarded-* /// family as reserved and silently strips any such header not set through its own mechanism. /// public static IApplicationBuilder UseGatewayPrefixForwarding(this IApplicationBuilder app) { return app.Use((context, next) => { if (context.Request.Headers.TryGetValue("X-GB5-Gateway-Prefix", out var prefix) && !string.IsNullOrEmpty(prefix)) context.Request.PathBase = new PathString(prefix.ToString()); return next(); }); } }