| | | 1 | | using Kestrun.Scripting; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Health; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Supported response content types for the built-in health endpoint. |
| | | 7 | | /// </summary> |
| | | 8 | | public enum HealthEndpointContentType |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Emits JSON output (default). |
| | | 12 | | /// </summary> |
| | | 13 | | Json, |
| | | 14 | | /// <summary> |
| | | 15 | | /// Emits YAML output. |
| | | 16 | | /// </summary> |
| | | 17 | | Yaml, |
| | | 18 | | /// <summary> |
| | | 19 | | /// Emits XML output. |
| | | 20 | | /// </summary> |
| | | 21 | | Xml, |
| | | 22 | | /// <summary> |
| | | 23 | | /// Emits a concise human-readable plain text summary. |
| | | 24 | | /// </summary> |
| | | 25 | | Text, |
| | | 26 | | /// <summary> |
| | | 27 | | /// Automatically negotiates based on the Accept header. |
| | | 28 | | /// </summary> |
| | | 29 | | Auto |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Options controlling the built-in health endpoint exposed by <see cref="Hosting.KestrunHost"/>. |
| | | 34 | | /// </summary> |
| | | 35 | | public sealed class HealthEndpointOptions |
| | | 36 | | { |
| | 1315 | 37 | | private string _pattern = "/health"; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets the relative route path the endpoint is exposed on. Defaults to <c>/health</c>. |
| | | 41 | | /// </summary> |
| | | 42 | | public string Pattern |
| | | 43 | | { |
| | 3 | 44 | | get => _pattern; |
| | 3 | 45 | | set => _pattern = string.IsNullOrWhiteSpace(value) |
| | 3 | 46 | | ? "/health" |
| | 3 | 47 | | : (value.StartsWith('/') ? value : "/" + value); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets or sets the default probe tags applied when a request does not provide an explicit tag filter. |
| | | 52 | | /// </summary> |
| | 1320 | 53 | | public string[] DefaultTags { get; set; } = []; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets or sets a value indicating whether anonymous callers can hit the endpoint. |
| | | 57 | | /// </summary> |
| | 1320 | 58 | | public bool AllowAnonymous { get; set; } = true; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets a value indicating whether degraded probes should cause the endpoint to return <c>503 ServiceUnavai |
| | | 62 | | /// </summary> |
| | 5 | 63 | | public bool TreatDegradedAsUnhealthy { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets a value indicating whether an exception should be thrown if an endpoint with the same pattern alrea |
| | | 67 | | /// </summary> |
| | 5 | 68 | | public bool ThrowOnDuplicate { get; set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets or sets additional authentication schemes required for the endpoint. Leave empty to inherit the application |
| | | 72 | | /// </summary> |
| | 1320 | 73 | | public string[] RequireSchemes { get; set; } = []; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets or sets additional authorization policies required for the endpoint. |
| | | 77 | | /// </summary> |
| | 1320 | 78 | | public string[] RequirePolicies { get; set; } = []; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets or sets the name of a CORS policy to apply, if any. |
| | | 82 | | /// </summary> |
| | 5 | 83 | | public string? CorsPolicyName { get; set; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets or sets the name of an ASP.NET Core rate limiting policy to apply, if any. |
| | | 87 | | /// </summary> |
| | 5 | 88 | | public string? RateLimitPolicyName { get; set; } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets or sets a value indicating whether the endpoint should short-circuit the rest of the pipeline. |
| | | 92 | | /// </summary> |
| | 5 | 93 | | public bool ShortCircuit { get; set; } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets or sets the status code returned when <see cref="ShortCircuit"/> is <c>true</c>. Defaults to <c>200</c> or |
| | | 97 | | /// </summary> |
| | 5 | 98 | | public int? ShortCircuitStatusCode { get; set; } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Gets or sets the OpenAPI summary applied to the endpoint metadata. |
| | | 102 | | /// </summary> |
| | 1320 | 103 | | public string? OpenApiSummary { get; set; } = "Aggregate health status."; |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets or sets the OpenAPI description applied to the endpoint metadata. |
| | | 107 | | /// </summary> |
| | 1320 | 108 | | public string? OpenApiDescription { get; set; } = "Returns the current reported state of all registered probes."; |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets or sets the OpenAPI operation id applied to the endpoint metadata. |
| | | 112 | | /// </summary> |
| | 1320 | 113 | | public string? OpenApiOperationId { get; set; } = "GetHealth"; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets or sets the OpenAPI tag list applied to the endpoint metadata. |
| | | 117 | | /// </summary> |
| | 1320 | 118 | | public string[] OpenApiTags { get; set; } = ["Health"]; |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Gets or sets the OpenAPI group name applied to the endpoint metadata. |
| | | 122 | | /// </summary> |
| | 5 | 123 | | public string? OpenApiGroupName { get; set; } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets or sets the maximum degree of parallelism used when executing probes. |
| | | 127 | | /// </summary> |
| | 1320 | 128 | | public int MaxDegreeOfParallelism { get; set; } = Math.Min(Environment.ProcessorCount, 4); |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Gets or sets the timeout applied to each individual probe execution. |
| | | 132 | | /// </summary> |
| | 1320 | 133 | | public TimeSpan ProbeTimeout { get; set; } = TimeSpan.FromSeconds(15); |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Gets or sets a value indicating whether the endpoint should be automatically registered when a host is created. |
| | | 137 | | /// </summary> |
| | 1320 | 138 | | public bool AutoRegisterEndpoint { get; set; } = true; |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets or sets the script language used when generating health check probes from script. Defaults to <see cref="Sc |
| | | 142 | | /// </summary> |
| | 1320 | 143 | | public ScriptLanguage DefaultScriptLanguage { get; set; } = ScriptLanguage.PowerShell; |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets or sets the response content type produced by the endpoint. |
| | | 147 | | /// </summary> |
| | 5 | 148 | | public HealthEndpointContentType ResponseContentType { get; set; } = HealthEndpointContentType.Json; |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets or sets the root element name used when emitting XML output. Defaults to <c>Response</c>. |
| | | 152 | | /// Ignored for non-XML output types. If null or whitespace a default of <c>Response</c> is used. |
| | | 153 | | /// </summary> |
| | 1317 | 154 | | public string? XmlRootElementName { get; set; } = "Response"; |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets or sets a value indicating whether the health response should be compressed/compact when using JSON or XML. |
| | | 158 | | /// When <c>false</c> (default) the output is human readable (indented / pretty). When <c>true</c> the output is |
| | | 159 | | /// compact (no unnecessary whitespace) which can reduce payload size for large probe data sets. |
| | | 160 | | /// </summary> |
| | 2 | 161 | | public bool Compress { get; set; } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Creates a deep copy of the current instance. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <returns>A cloned <see cref="HealthEndpointOptions"/> instance.</returns> |
| | 1 | 167 | | public HealthEndpointOptions Clone() => new() |
| | 1 | 168 | | { |
| | 1 | 169 | | Pattern = Pattern, |
| | 1 | 170 | | DefaultTags = [.. DefaultTags], |
| | 1 | 171 | | AllowAnonymous = AllowAnonymous, |
| | 1 | 172 | | TreatDegradedAsUnhealthy = TreatDegradedAsUnhealthy, |
| | 1 | 173 | | ThrowOnDuplicate = ThrowOnDuplicate, |
| | 1 | 174 | | RequireSchemes = [.. RequireSchemes], |
| | 1 | 175 | | RequirePolicies = [.. RequirePolicies], |
| | 1 | 176 | | CorsPolicyName = CorsPolicyName, |
| | 1 | 177 | | RateLimitPolicyName = RateLimitPolicyName, |
| | 1 | 178 | | ShortCircuit = ShortCircuit, |
| | 1 | 179 | | ShortCircuitStatusCode = ShortCircuitStatusCode, |
| | 1 | 180 | | OpenApiSummary = OpenApiSummary, |
| | 1 | 181 | | OpenApiDescription = OpenApiDescription, |
| | 1 | 182 | | OpenApiOperationId = OpenApiOperationId, |
| | 1 | 183 | | OpenApiTags = [.. OpenApiTags], |
| | 1 | 184 | | OpenApiGroupName = OpenApiGroupName, |
| | 1 | 185 | | MaxDegreeOfParallelism = MaxDegreeOfParallelism, |
| | 1 | 186 | | ProbeTimeout = ProbeTimeout, |
| | 1 | 187 | | AutoRegisterEndpoint = AutoRegisterEndpoint, |
| | 1 | 188 | | DefaultScriptLanguage = DefaultScriptLanguage, |
| | 1 | 189 | | ResponseContentType = ResponseContentType, |
| | 1 | 190 | | XmlRootElementName = XmlRootElementName, |
| | 1 | 191 | | Compress = Compress |
| | 1 | 192 | | }; |
| | | 193 | | } |