Skip to content

Commit

Permalink
VCST-206: Proxy XAPI subscription websocket connections (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvishnyakov authored Mar 26, 2024
1 parent 7a6776a commit ab97932
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions VirtoCommerce.Storefront/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;
using FluentValidation.AspNetCore;
using GraphQL.Client.Abstractions;
using GraphQL.Client.Http;
Expand Down Expand Up @@ -340,6 +342,8 @@ public void ConfigureServices(IServiceCollection services)

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

services.Configure<ProxyOptions>(options => options.WebSocketKeepAliveInterval = TimeSpan.FromSeconds(50));

services.AddSingleton<IGraphQLClient>(s =>
{
var platformEndpointOptions = s.GetRequiredService<IOptions<PlatformEndpointOptions>>().Value;
Expand Down Expand Up @@ -427,16 +431,22 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
});

var platformEndpointOptions = app.ApplicationServices.GetRequiredService<IOptions<PlatformEndpointOptions>>().Value;
// Forwards the request only when the host is set to the specified value
app.UseWhen(
context => context.Request.Path.Value.EndsWith("xapi/graphql"),
appInner => appInner.RunProxy(context =>

var httpsPlatformGraphqlEndpoint = new Uri(platformEndpointOptions.Url, "graphql");
var wssPlatformGraphqlEndpoint = new UriBuilder(httpsPlatformGraphqlEndpoint)
{
Scheme = httpsPlatformGraphqlEndpoint.Scheme == Uri.UriSchemeHttps ? Uri.UriSchemeWss : Uri.UriSchemeWs
}.Uri;

app.UseWebSockets();
app.Map("/xapi/graphql",
appInner =>
{
context.Request.Path = PathString.Empty;
return context.ForwardTo(new Uri(platformEndpointOptions.Url, "graphql"))
appInner.UseWebSocketProxy(_ => wssPlatformGraphqlEndpoint, options => options.AddXForwardedHeaders());
appInner.RunProxy(context => context.ForwardTo(httpsPlatformGraphqlEndpoint)
.AddXForwardedHeaders()
.Send();
}));
.Send());
});

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

0 comments on commit ab97932

Please sign in to comment.