2using System.Globalization;
5using Microsoft.AspNetCore.Builder;
6using Microsoft.AspNetCore.Http;
7using Microsoft.AspNetCore.Mvc;
8using Microsoft.EntityFrameworkCore;
9using Microsoft.Extensions.DependencyInjection;
10using Microsoft.Extensions.Logging;
30 internal static bool LogSwarmIdentifier {
get;
set; }
38 ArgumentNullException.ThrowIfNull(applicationBuilder);
40 applicationBuilder.Use(async (context, next) =>
47 catch (DbUpdateException e)
49 if (e.InnerException is OperationCanceledException)
51 logger.LogTrace(e,
"Rethrowing DbUpdateException as OperationCanceledException");
52 throw e.InnerException;
55 logger.LogDebug(e,
"Database conflict!");
58 AdditionalData = String.Format(CultureInfo.InvariantCulture, (e.InnerException ?? e).Message),
59 }).ExecuteResultAsync(
new ActionContext
61 HttpContext = context,
73 ArgumentNullException.ThrowIfNull(applicationBuilder);
74 applicationBuilder.Use(async (context, next) =>
81 catch (OperationCanceledException ex)
83 logger.LogDebug(ex,
"Request cancelled!");
94 ArgumentNullException.ThrowIfNull(applicationBuilder);
96 applicationBuilder.Use(async (context, next) =>
105 logger.LogError(e,
"Failed request!");
106 await
new JsonResult(
109 AdditionalData = e.ToString(),
112 StatusCode = (int)HttpStatusCode.InternalServerError,
114 .ExecuteResultAsync(
new ActionContext
116 HttpContext = context,
128 ArgumentNullException.ThrowIfNull(applicationBuilder);
130 applicationBuilder.Use(async (context, next) =>
132 var apiHeadersProvider = context.RequestServices.GetRequiredService<
IApiHeadersProvider>();
133 if (apiHeadersProvider.ApiHeaders?.Compatible() ==
false)
135 await
new BadRequestObjectResult(
137 .ExecuteResultAsync(
new ActionContext
139 HttpContext = context,
155 ArgumentNullException.ThrowIfNull(applicationBuilder);
156 ArgumentNullException.ThrowIfNull(assemblyInformationProvider);
158 applicationBuilder.Use((context, next) =>
160 context.Response.Headers.Add(
"X-Powered-By", assemblyInformationProvider.
VersionPrefix);
172 ArgumentNullException.ThrowIfNull(applicationBuilder);
175 applicationBuilder.Use((context, next) =>
177 context.Response.Headers.Add(
"X-Accel-Buffering",
"no");
189 ArgumentNullException.ThrowIfNull(applicationBuilder);
190 ArgumentNullException.ThrowIfNull(swarmConfiguration);
192 if (LogSwarmIdentifier && swarmConfiguration.
Identifier !=
null)
193 applicationBuilder.Use(async (context, next) =>
string? Identifier
The server's identifier.
Represents an error message returned by the server.
Configuration for the server swarm system.
Extensions for IApplicationBuilder.
static void UseServerBranding(this IApplicationBuilder applicationBuilder, IAssemblyInformationProvider assemblyInformationProvider)
Add the X-Powered-By response header.
static void UseDisabledNginxProxyBuffering(this IApplicationBuilder applicationBuilder)
Add the X-Accel-Buffering response header.
static void UseServerErrorHandling(this IApplicationBuilder applicationBuilder)
Suppress all in flight Exceptions for the request with error 500.
static ILogger GetLogger(HttpContext httpContext)
Gets a ILogger from a given httpContext .
static void UseDbConflictHandling(this IApplicationBuilder applicationBuilder)
Return a ConflictObjectResult for DbUpdateExceptions.
static void UseCancelledRequestSuppression(this IApplicationBuilder applicationBuilder)
Suppress global::System.Threading.Tasks.TaskCanceledException warnings when a user aborts a request.
static void UseAdditionalRequestLoggingContext(this IApplicationBuilder applicationBuilder, SwarmConfiguration swarmConfiguration)
Adds additional global LogContext to the request pipeline.
static void UseApiCompatibility(this IApplicationBuilder applicationBuilder)
Check that the API version is the current major version if it's present in the headers.
Helpers for manipulating the Serilog.Context.LogContext.
const string SwarmIdentifierContextProperty
The Serilog.Context.LogContext property name for Api.Models.Internal.SwarmServer.Identifiers.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.