Skip to content

Commit ab97932

Browse files
authored
VCST-206: Proxy XAPI subscription websocket connections (#676)
1 parent 7a6776a commit ab97932

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

VirtoCommerce.Storefront/Startup.cs

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Text.Encodings.Web;
56
using System.Text.Unicode;
7+
using System.Threading.Tasks;
68
using FluentValidation.AspNetCore;
79
using GraphQL.Client.Abstractions;
810
using GraphQL.Client.Http;
@@ -340,6 +342,8 @@ public void ConfigureServices(IServiceCollection services)
340342

341343
services.AddProxy(builder => builder.AddHttpMessageHandler(sp => sp.GetService<AuthenticationHandlerFactory>().CreateAuthHandler()));
342344

345+
services.Configure<ProxyOptions>(options => options.WebSocketKeepAliveInterval = TimeSpan.FromSeconds(50));
346+
343347
services.AddSingleton<IGraphQLClient>(s =>
344348
{
345349
var platformEndpointOptions = s.GetRequiredService<IOptions<PlatformEndpointOptions>>().Value;
@@ -427,16 +431,22 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
427431
});
428432

429433
var platformEndpointOptions = app.ApplicationServices.GetRequiredService<IOptions<PlatformEndpointOptions>>().Value;
430-
// Forwards the request only when the host is set to the specified value
431-
app.UseWhen(
432-
context => context.Request.Path.Value.EndsWith("xapi/graphql"),
433-
appInner => appInner.RunProxy(context =>
434+
435+
var httpsPlatformGraphqlEndpoint = new Uri(platformEndpointOptions.Url, "graphql");
436+
var wssPlatformGraphqlEndpoint = new UriBuilder(httpsPlatformGraphqlEndpoint)
437+
{
438+
Scheme = httpsPlatformGraphqlEndpoint.Scheme == Uri.UriSchemeHttps ? Uri.UriSchemeWss : Uri.UriSchemeWs
439+
}.Uri;
440+
441+
app.UseWebSockets();
442+
app.Map("/xapi/graphql",
443+
appInner =>
434444
{
435-
context.Request.Path = PathString.Empty;
436-
return context.ForwardTo(new Uri(platformEndpointOptions.Url, "graphql"))
445+
appInner.UseWebSocketProxy(_ => wssPlatformGraphqlEndpoint, options => options.AddXForwardedHeaders());
446+
appInner.RunProxy(context => context.ForwardTo(httpsPlatformGraphqlEndpoint)
437447
.AddXForwardedHeaders()
438-
.Send();
439-
}));
448+
.Send());
449+
});
440450

441451
app.UseWhen(
442452
context => context.Request.Path.Value.EndsWith("/token"),

0 commit comments

Comments
 (0)