Esempio n. 1
0
        private async Task HandleEventSubsciptionAsync(IServiceDefinition serviceDefinition, string eventName, HttpContext context, CancellationToken ct)
        {
            if (context.Request.Method != "PUT" && context.Request.Method != "POST")
            {
                context.Response.StatusCode  = 405; // Method Not Allowed
                context.Response.ContentType = "text/plain";
                await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes("To subscribe to an event, use one of these HTTP verbs: GET, POST, PUT"));

                return;
            }

            var serviceName = (context.Request.Query.TryGetValue("service", out var serviceValues) && serviceValues.Count == 1) ? serviceValues[0] : null;

            if (string.IsNullOrWhiteSpace(serviceName))
            {
                context.Response.StatusCode  = 404;
                context.Response.ContentType = "text/plain";
                await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes("Missing 'service=xxx' in the URL query."));

                return;
            }

            var proxyName = (context.Request.Query.TryGetValue("proxy", out var proxyValues) && proxyValues.Count == 1) ? proxyValues[0] : null;

            var subscriberServiceId = new ServiceId {
                ServiceName = serviceName, ProxyName = proxyName
            };

            var eventId = new EventId {
                EventName = eventName
            };
            var publisherServiceId = new ServiceId {
                ServiceName = serviceDefinition.Name
            };
            var eventDesc = new EventDescriptor {
                ServiceId = publisherServiceId, EventId = eventId
            };

            _eventDispatcher.OnSubscriberAdded(eventDesc, subscriberServiceId);

            context.Response.StatusCode = 200;
        }