Repository navigation
Expand file tree
/
Copy pathSettingsMiddleware.cs
More file actions
110 lines (92 loc) · 3.65 KB
/
Copy pathSettingsMiddleware.cs
File metadata and controls
110 lines (92 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
namespace HotChocolate.AspNetCore.Playground
{
internal sealed class SettingsMiddleware
{
private readonly PlaygroundOptions _options;
private readonly string _queryPath;
private readonly string _subscriptionPath;
public SettingsMiddleware(
RequestDelegate next,
PlaygroundOptions options)
{
Next = next;
_options = options
?? throw new ArgumentNullException(nameof(options));
Uri uiPath = UriFromPath(options.Path);
Uri queryPath = UriFromPath(options.QueryPath);
Uri subscriptionPath = UriFromPath(options.SubscriptionPath);
_queryPath = uiPath.MakeRelativeUri(queryPath).ToString();
_subscriptionPath = uiPath.MakeRelativeUri(subscriptionPath).ToString();
}
internal RequestDelegate Next { get; }
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task InvokeAsync(HttpContext context)
{
string queryUrl = BuildUrl(_options.GraphQLEndpoint, context.Request, false, _queryPath);
string subscriptionUrl = _options.EnableSubscription
? $"\"{BuildUrl(_options.GraphQLEndpoint, context.Request, true, _subscriptionPath)}\""
: "null";
string callbackUrl = BuildUrl(_options.GraphQLEndpoint, context.Request, false, "callback");
context.Response.ContentType = "application/javascript";
await context.Response.WriteAsync($@"
window.Settings = {{
url: ""{queryUrl}"",
subscriptionUrl: {subscriptionUrl},
path: ""{_options.Path}"",
opendIdConfiguration: {{
client_id: ""{_options.OpenIdConfiguration.ClientId}"",
redirect_uri: ""{callbackUrl}"",
response_type: ""{_options.OpenIdConfiguration.ResponseType}"",
scope: ""{_options.OpenIdConfiguration.Scope}"",
authority: ""{_options.OpenIdConfiguration.Authority}""
}}
}}
",
context.RequestAborted)
.ConfigureAwait(false);
}
private static string BuildUrl(
HttpRequest request,
bool websocket,
string path)
{
string uiPath = request.PathBase.Value
.Substring(0, request.PathBase.Value.Length - 11);
string scheme = request.Scheme;
if (websocket)
{
scheme = request.IsHttps ? "wss" : "ws";
}
return UriHelper.BuildAbsolute(
scheme, request.Host, uiPath + path)
.TrimEnd('/');
}
private static string BuildUrl(Uri? uri, HttpRequest request, bool websocket, string path)
{
if (uri is null)
return BuildUrl(request, websocket, path);
if (!websocket)
return uri.AbsoluteUri;
var builder = new UriBuilder(uri)
{
Scheme = uri.Scheme == Uri.UriSchemeHttps ? "wss" : "ws"
};
return builder.Uri.AbsoluteUri;
}
private static Uri UriFromPath(PathString path)
{
return new Uri(
"http://p" +
(path.HasValue ? path.Value : "/").TrimEnd('/') +
"/");
}
}
}