public RouteBuilder(IContainer container, IAssemblyScanner assemblyScanner, IAddressProvider addressProvider, MessageConventions conventions)
 {
     this.container       = container;
     this.assemblyScanner = assemblyScanner;
     this.addressProvider = addressProvider;
     this.conventions     = conventions;
 }
Exemplo n.º 2
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (!_endpoints.TryGetValue(context.Request.Method, out var endpoints))
            {
                await next(context);
                return;
            }

            foreach (var endpoint in endpoints)
            {
                var match = _routeMatcher.Match(endpoint.Path, context.Request.Path);
                if (match is null)
                {
                    continue;
                }

                var key = $"{endpoint.Exchange}:{endpoint.RoutingKey}";
                if (!Conventions.TryGetValue(key, out var conventions))
                {
                    conventions = new MessageConventions(typeof(object), endpoint.RoutingKey, endpoint.Exchange, null);
                    Conventions.TryAdd(key, conventions);
                }

                var messageId = Guid.NewGuid().ToString("N");
                var correlationId = _correlationIdFactory.Create();
                var resourceId = Guid.NewGuid().ToString("N");
                var correlationContext = _correlationContextBuilder.Build(context, correlationId, default,
Exemplo n.º 3
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (_routes is null || !_routes.Any())
            {
                await next(context);

                return;
            }

            var key = GetKey(context);

            if (!_routes.TryGetValue(key, out var route))
            {
                await next(context);

                return;
            }

            if ((_authenticate && route.Authenticate != false || route.Authenticate == true) &&
                !_anonymousRouteValidator.HasAccess(context.Request.Path))
            {
                var authenticateResult = await context.AuthenticateAsync();

                if (!authenticateResult.Succeeded)
                {
                    context.Response.StatusCode = 401;
                    return;
                }

                context.User = authenticateResult.Principal;
            }

            if (!Conventions.TryGetValue(key, out var conventions))
            {
                conventions = new MessageConventions(typeof(object), route.RoutingKey, route.Exchange, null);
                Conventions.TryAdd(key, conventions);
            }

            var spanContext = _tracer.ActiveSpan is null ? string.Empty : _tracer.ActiveSpan.Context.ToString();
            var message     = await _payloadBuilder.BuildFromJsonAsync <object>(context.Request);

            var resourceId = Guid.NewGuid().ToString("N");

            if (context.Request.Method == "POST" && message is JObject jObject)
            {
                jObject.SetResourceId(resourceId);
            }

            var messageId          = Guid.NewGuid().ToString("N");
            var correlationId      = Guid.NewGuid().ToString("N");
            var correlationContext = _correlationContextBuilder.Build(context, correlationId, spanContext,
                                                                      route.RoutingKey, resourceId);

            _rabbitMqClient.Send(message, conventions, messageId, correlationId, spanContext, correlationContext);
            context.Response.StatusCode = 202;
            context.Response.SetOperationHeader(correlationId);
        }