Beispiel #1
0
        public static void UseHealthEndpoint(this IApplicationBuilder app)
        {
            app.Use(async(context, next) =>
            {
                if (context.Request.Path.Value.Equals("/health"))
                {
                    // Perform IP access check
                    if (MicroserviceConfiguration.AllowedIpAddresses != null && context.Request.HttpContext.Connection.RemoteIpAddress != null)
                    {
                        if (!MicroserviceConfiguration.AllowedIpAddresses.Contains(context.Request.HttpContext.Connection.RemoteIpAddress))
                        {
                            context.Response.StatusCode = 403;
                            await next();
                        }
                    }

                    HealthCheckRegistry.HealthStatus status = await Task.Run(() => HealthCheckRegistry.GetStatus());

                    if (!status.IsHealthy)
                    {
                        // Return a service unavailable status code if any of the checks fail
                        context.Response.StatusCode = 503;
                    }

                    context.Response.Headers["Content-Type"] = "application/json";
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(status));
                }
                else
                {
                    await next();
                }
            });
        }
Beispiel #2
0
        public static void UseHealthEndpoint(this IAppBuilder app)
        {
            app.Use(async(context, next) =>
            {
                if (context.Request.Path.Value.StartsWith("/health"))
                {
                    HealthCheckRegistry.HealthStatus status = HealthCheckRegistry.GetStatus();

                    if (!status.IsHealthy)
                    {
                        // Return a service unavailable status code if any of the checks fail
                        context.Response.StatusCode = 503;
                    }

                    context.Response.Headers.Set("Content-Type", "application/json");
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(status));
                }
                else
                {
                    await next();
                }
            });
        }