示例#1
0
        public async Task RouteAsync(RouteContext context)
        {
            if (context.HttpContext.Request.Method == HttpMethods.Post)
            {
                var segments =
                    context.HttpContext
                    .Request
                    .Path
                    .Value
                    .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (segments[0] == "discovery")
                {
                    using var reader = new StreamReader(context.HttpContext.Request.Body);
                    var source = await reader.ReadToEndAsync();

                    var apiUri = new Uri(source);
                    _frontendEnvironment.SetApiUri(apiUri);

                    context.Handler = async httpContext =>
                    {
                        await httpContext.Response.CompleteAsync();
                    };
                }
            }
        }
        private async Task HandlePostVerb(RouteContext context)
        {
            var segments =
                context.HttpContext
                .Request
                .Path
                .Value
                .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (segments[0] == "apitunnel")
            {
                using var reader = new StreamReader(context.HttpContext.Request.Body);
                var source = await reader.ReadToEndAsync();

                var requestBody = JObject.Parse(source);

                var apiUri          = new Uri(requestBody["tunnelUri"].Value <string>());
                var frontendType    = requestBody["frontendType"].Value <string>();
                var hash            = $"{Guid.NewGuid():N}";
                var bootstrapperUri = new Uri(apiUri, $"apitunnel/{frontendType}/{hash}/bootstrapper.js");
                _frontendEnvironment.SetApiUri(apiUri);

                _bootstrapperScripts.GetOrAdd(bootstrapperUri, key => GenerateBootstrapperCode(apiUri, frontendType, hash));

                context.Handler = async httpContext =>
                {
                    httpContext.Response.ContentType = "text/plain";
                    var response = new JObject
                    {
                        { "bootstrapperUri", bootstrapperUri.ToString() }
                    };
                    await httpContext.Response.WriteAsync(response.ToString(Newtonsoft.Json.Formatting.None));

                    await httpContext.Response.CompleteAsync();
                };
            }
        }