Example #1
0
        /// <summary>
        /// Processes a request and sets the logging context for the specified <see cref="ServiceDescriptor"/>.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
        /// <param name="serviceDescriptor">The <see cref="serviceDescriptor"/>.</param>
        /// <param name="features"></param>
        public async Task Invoke(HttpContext context, ServiceDescriptor serviceDescriptor, IEnumerable <NaosFeatureInformation> features)
        {
            using (this.logger.BeginScope(new Dictionary <string, object>
            {
                [LogPropertyKeys.ServiceProduct] = serviceDescriptor.Product,
                [LogPropertyKeys.ServiceCapability] = serviceDescriptor.Capability,
                [LogPropertyKeys.ServiceName] = serviceDescriptor.Name,
            }))
            {
                context.SetServiceName(serviceDescriptor.Name);
                // TODO: log below should take blacklistpatterns in account (RequestResponseLoggingOptions)
                //this.logger.LogInformation($"SERVICE http request  ({context.GetRequestId()}) service={serviceDescriptor.Name}, tags={string.Join("|", serviceDescriptor.Tags.NullToEmpty())}");

                //await this.next(context);

                if (context.Request.Method.SafeEquals(HttpMethods.Get) &&
                    (context.Request.Path.SafeEquals("/") || context.Request.Path.SafeEquals("/index.html")))
                {
                    await context.Response.WriteNaosDashboard(
                        title : serviceDescriptor?.ToString(),
                        tags : serviceDescriptor?.Tags).AnyContext();
                }
                else if (context.Request.Method.SafeEquals(HttpMethods.Get) &&
                         context.Request.Path.SafeEquals("/css/naos/styles.css"))
                {
                    context.Response.ContentType = ContentType.CSS.ToValue();
                    await context.Response.WriteAsync(ResourcesHelper.GetStylesAsString()).AnyContext();
                }
                else if (context.Request.Method.SafeEquals(HttpMethods.Get) &&
                         context.Request.Path.SafeEquals("/css/naos/swagger.css"))
                {
                    context.Response.ContentType = ContentType.CSS.ToValue();
                    await context.Response.WriteAsync(ResourcesHelper.GetSwaggerStylesAsString()).AnyContext();
                }
                else if (context.Request.Method.SafeEquals(HttpMethods.Get) &&
                         context.Request.Path.SafeEquals("/favicon.ico"))
                {
                    context.Response.ContentType = ContentType.ICO.ToValue();
                    var icon = ResourcesHelper.GetIconAsBytes();
                    await context.Response.Body.WriteAsync(icon, 0, icon.Length).AnyContext();
                }
                else if (context.Request.Method.SafeEquals(HttpMethods.Get) &&
                         context.Request.Path == "/error")
                {
                    throw new NaosException("forced exception");
                }
                else
                {
                    await this.next(context).AnyContext();
                }
            }
        }