Exemplo n.º 1
0
        /// <summary>
        /// Runs all of the health check steps that are defined in our options
        /// and returns a response object that we can send
        /// </summary>
        /// <returns>The response object containing the health of our service</returns>
        public static async Task <HealthCheckResponse> GetHealthResponseAsync(
            this GenkiOptions options, IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            // Run through our health check steps
            var resultTasks = serviceProvider.GetHealthCheckSteps()

                              // For each step create an object containing the details and the result
                              .Select(async s => new HealthCheckStepResponse
            {
                Name        = s.Name,
                Description = s.Description,
                Importance  = s.Importance,
                IsHealthy   = await s.GetIsHealthyAsync()
            });

            // Get completed results
            var results = await Task.WhenAll(resultTasks);

            return(new HealthCheckResponse
            {
                Service = options.ServiceName,
                Steps = results
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the endpoint that we will respond to requests on
        /// </summary>
        /// <returns>The endpoint that we will respond to requests on</return>
        public static string GetEndpoint(this GenkiOptions options)
        {
            // Get either the endpoint defined by the options or the DefaultEndpoint
            // if none is specified
            var endpoint = string.IsNullOrEmpty(options.Endpoint) ?
                           DefaultEndpoint : options.Endpoint;

            // if the endpoint starts with / then return as we can use as is
            if (endpoint.StartsWith("/"))
            {
                return(endpoint);
            }

            // Return our endpoint with a / at the start
            return($"/{endpoint}");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds Genki to the DI container, giving us another builder to attach custom health checks
        /// </summary>
        /// <param name="serviceCollection">A collection of service descriptors</param>
        /// <param name="options">Options for the health check</param>
        public static IServiceCollection AddGenki(
            this IServiceCollection serviceCollection, Action <GenkiOptions> options)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var genkiOptions = new GenkiOptions();

            // Set up our options
            options(genkiOptions);

            // Add to service collection
            serviceCollection.AddSingleton(genkiOptions);

            return(serviceCollection);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new instance of <see cref="Genki" />
        /// </summary>
        /// <param name="next">The delegate representing the next middleware in the request pipeline</param>
        /// <param name="options">The middleware options</param>
        /// <param name="serviceProvider">The provider for retrieving a service object</param>
        public GenkiMiddleware(
            RequestDelegate next,
            GenkiOptions options,
            IServiceProvider serviceProvider)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            _next            = next;
            _options         = options;
            _serviceProvider = serviceProvider;
        }