|
| 1 | +using System.Net; |
| 2 | +using System.Net.Security; |
| 3 | +using Microsoft.AspNetCore.Http.Extensions; |
| 4 | +using Yarp.ReverseProxy.Forwarder; |
| 5 | + |
| 6 | +namespace Account.Api; |
| 7 | + |
| 8 | +// Dev-only proxy that forwards back-office static-asset and HMR traffic from the back-office Kestrel |
| 9 | +// listener (BACK_OFFICE_KESTREL_PORT) to the rsbuild dev server (BACK_OFFICE_STATIC_PORT). Production |
| 10 | +// builds bake the bundle into BackOfficeWebApp/dist and serve it via UseStaticFiles, so this proxy |
| 11 | +// is registered only in development. The proxy must run before authentication so anonymous browsers |
| 12 | +// can fetch /static/*, /rsbuild-hmr, and HMR /{filename}.hot-update.{ext} during the SPA bootstrap. |
| 13 | +public static class BackOfficeDevStaticProxy |
| 14 | +{ |
| 15 | + public static IServiceCollection AddBackOfficeDevStaticProxy(this IServiceCollection services) |
| 16 | + { |
| 17 | + services.AddHttpForwarder(); |
| 18 | + return services; |
| 19 | + } |
| 20 | + |
| 21 | + public static IApplicationBuilder UseBackOfficeDevStaticProxy(this WebApplication app, string backOfficeHostname) |
| 22 | + { |
| 23 | + if (!app.Environment.IsDevelopment()) return app; |
| 24 | + |
| 25 | + var rawPort = Environment.GetEnvironmentVariable("BACK_OFFICE_STATIC_PORT"); |
| 26 | + if (!int.TryParse(rawPort, out var staticPort) || staticPort <= 0) return app; |
| 27 | + |
| 28 | + var destinationPrefix = $"https://localhost:{staticPort}"; |
| 29 | + var forwarder = app.Services.GetRequiredService<IHttpForwarder>(); |
| 30 | + var invoker = new HttpMessageInvoker(new SocketsHttpHandler |
| 31 | + { |
| 32 | + UseProxy = false, |
| 33 | + AllowAutoRedirect = false, |
| 34 | + AutomaticDecompression = DecompressionMethods.None, |
| 35 | + UseCookies = false, |
| 36 | + // The rsbuild dev server uses a self-signed certificate from Aspire; trust it locally. |
| 37 | + SslOptions = new SslClientAuthenticationOptions |
| 38 | + { |
| 39 | + RemoteCertificateValidationCallback = (_, _, _, _) => true |
| 40 | + } |
| 41 | + } |
| 42 | + ); |
| 43 | + |
| 44 | + app.UseWhen( |
| 45 | + context => IsBackOfficeStaticRequest(context, backOfficeHostname), |
| 46 | + branch => branch.Run(async context => |
| 47 | + { |
| 48 | + var error = await forwarder.SendAsync(context, destinationPrefix, invoker, ForwarderRequestConfig.Empty); |
| 49 | + if (error != ForwarderError.None && !context.Response.HasStarted) |
| 50 | + { |
| 51 | + var feature = context.Features.Get<IForwarderErrorFeature>(); |
| 52 | + app.Logger.LogWarning( |
| 53 | + feature?.Exception, |
| 54 | + "Back-office dev static proxy failed for {Url}: {Error}", |
| 55 | + context.Request.GetDisplayUrl(), |
| 56 | + error |
| 57 | + ); |
| 58 | + context.Response.StatusCode = StatusCodes.Status502BadGateway; |
| 59 | + } |
| 60 | + } |
| 61 | + ) |
| 62 | + ); |
| 63 | + return app; |
| 64 | + } |
| 65 | + |
| 66 | + private static bool IsBackOfficeStaticRequest(HttpContext context, string backOfficeHostname) |
| 67 | + { |
| 68 | + if (!context.Request.Host.Host.Equals(backOfficeHostname, StringComparison.OrdinalIgnoreCase)) return false; |
| 69 | + |
| 70 | + var path = context.Request.Path.Value; |
| 71 | + if (string.IsNullOrEmpty(path)) return false; |
| 72 | + |
| 73 | + // /static/* covers all bundled JS/CSS chunks. /rsbuild-hmr is rsbuild's HMR WebSocket endpoint |
| 74 | + // (HTTP upgrade is handled by IHttpForwarder). The .hot-update.{ext} pattern catches HMR-delta |
| 75 | + // chunks rsbuild emits at the SPA root rather than under /static. |
| 76 | + if (path.StartsWith("/static/", StringComparison.OrdinalIgnoreCase)) return true; |
| 77 | + if (path.StartsWith("/rsbuild-hmr", StringComparison.OrdinalIgnoreCase)) return true; |
| 78 | + if (path.Contains(".hot-update.", StringComparison.OrdinalIgnoreCase)) return true; |
| 79 | + |
| 80 | + return false; |
| 81 | + } |
| 82 | +} |
0 commit comments