Beispiel #1
0
        public async Task HandleAsync(HttpContext context, RouteConfig config)
        {
            if (config.Route.Downstream is null)
            {
                return;
            }

            var executionData = await _requestProcessor.ProcessAsync(config, context);

            if (!executionData.IsPayloadValid)
            {
                await _payloadValidator.TryValidate(executionData, context.Response);

                return;
            }

            if (string.IsNullOrWhiteSpace(executionData.Downstream))
            {
                return;
            }

            var method = config.Route.Method.ToUpperInvariant();

            _logger.LogInformation($"Sending HTTP {method} request to: {config.Downstream} " +
                                   $"[Trace ID: {context.TraceIdentifier}]");
            var httpResponse = SendRequestAsync(executionData);

            if (httpResponse is null)
            {
                return;
            }

            await WriteResponseAsync(context.Response, await httpResponse(), executionData);
        }
Beispiel #2
0
        public async Task HandleAsync(HttpRequest request, HttpResponse response, RouteData data, RouteConfig config)
        {
            var executionData = await _requestProcessor.ProcessAsync(config, request, response, data);

            if (!executionData.IsPayloadValid)
            {
                await _payloadValidator.TryValidate(executionData, response);

                return;
            }

            var traceId     = request.HttpContext.TraceIdentifier;
            var routeConfig = executionData.Route.Config;
            var routingKey  = routeConfig[ConfigRoutingKey];
            var exchange    = routeConfig[ConfigExchange];
            var message     = executionData.HasPayload
                ? executionData.Payload
                : await _payloadBuilder.BuildJsonAsync <object>(request);

            var context    = _contextBuilder.Build(executionData);
            var hasTraceId = !string.IsNullOrWhiteSpace(traceId);

            _logger.LogInformation($"Sending a message: {routingKey} to the exchange: {exchange}" +
                                   (hasTraceId ? $" [Trace ID: {traceId}]" : string.Empty));

            _rabbitMqClient.Send(message, routingKey, exchange, context);

            if (!string.IsNullOrWhiteSpace(executionData.RequestId))
            {
                response.Headers.Add(RequestIdHeader, executionData.RequestId);
            }

            if (!string.IsNullOrWhiteSpace(executionData.ResourceId) && executionData.Route.Method is "post")
            {
                response.Headers.Add(ResourceIdHeader, executionData.ResourceId);
            }

            if (hasTraceId)
            {
                response.Headers.Add(TraceIdHeader, traceId);
            }

            response.StatusCode = 202;
        }
Beispiel #3
0
        public async Task HandleAsync(HttpContext context, RouteConfig config)
        {
            var executionData = await _requestProcessor.ProcessAsync(config, context);

            if (!executionData.IsPayloadValid)
            {
                await _payloadValidator.TryValidate(executionData, context.Response);

                return;
            }

            var traceId     = context.TraceIdentifier;
            var routeConfig = executionData.Route.Config;
            var routingKey  = routeConfig[ConfigRoutingKey];
            var exchange    = routeConfig[ConfigExchange];
            var message     = executionData.HasPayload
                ? executionData.Payload
                : await _payloadBuilder.BuildJsonAsync <object>(context.Request);

            var messageContext = _contextBuilder.Build(executionData);
            var hasTraceId     = !string.IsNullOrWhiteSpace(traceId);
            var spanContext    = _spanContextBuilder.Build(executionData);

            _rabbitMqClient.Send(message, routingKey, exchange, spanContext: spanContext,
                                 messageContext: messageContext);

            if (!string.IsNullOrWhiteSpace(executionData.RequestId))
            {
                context.Response.Headers.Add(RequestIdHeader, executionData.RequestId);
            }

            if (!string.IsNullOrWhiteSpace(executionData.ResourceId) && executionData.Route.Method is "post")
            {
                context.Response.Headers.Add(ResourceIdHeader, executionData.ResourceId);
            }

            if (hasTraceId)
            {
                context.Response.Headers.Add(TraceIdHeader, traceId);
            }

            context.Response.StatusCode = 202;
        }