Skip to content

Commit

Permalink
use the same json options everywhere, add brotli compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Kürzdörfer committed Nov 26, 2024
1 parent 2c206ba commit 37b5bbd
Showing 1 changed file with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;
using System.IO.Compression;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text.Json;
using AspNetCoreRateLimit;
using Chronos;
using Chronos.Abstractions;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void Configure()
ConfigurePlugins();
}

internal void ConfigurePlugins()
private void ConfigurePlugins()
{
_implementationResolver
.FindPlugins<IPluginServiceConfiguration>()
Expand All @@ -73,7 +73,7 @@ internal void ConfigurePlugins()
});
}

internal void ConfigureHealthChecks()
private void ConfigureHealthChecks()
{
var healthChecksBuilder = _serviceCollection.AddHealthChecks();

Expand Down Expand Up @@ -106,7 +106,7 @@ from healthCheck in _implementationResolver.FindTypes(f =>
});
}

internal void ConfigureMetrics()
private void ConfigureMetrics()
{
// add metrics
_serviceCollection.AddOpenTelemetry().WithMetrics(
Expand Down Expand Up @@ -170,7 +170,7 @@ internal void ConfigureTracing()
ActivitySource.AddActivityListener(activityListener);
}

internal void ConfigureWebServices()
private void ConfigureWebServices()
{
_serviceCollection.AddSingleton<GlobalStatusCodeOptions>();
_serviceCollection.AddHttpContextAccessor();
Expand All @@ -179,24 +179,35 @@ internal void ConfigureWebServices()
corsPolicy.SetSanityDefaults();
_serviceCollection.AddCors(opts => opts.AddPolicy(PiBoxWebHostDefaults.CorsPolicyName, corsPolicy));

_serviceCollection.AddResponseCompression(options =>
_serviceCollection.AddResponseCompression(opts =>
{
options.EnableForHttps = true;
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = new[] { "application/json" };
opts.EnableForHttps = true;
opts.Providers.Clear();
opts.Providers.Add<BrotliCompressionProvider>();
opts.Providers.Add<GzipCompressionProvider>();
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat([
"image/svg+xml",
"image/x-icon",
"image/bmp",
"image/tiff",
"application/graphql-response+json"
]);
});
_serviceCollection.Configure<GzipCompressionProviderOptions>(options => { options.Level = CompressionLevel.Fastest; });
var mcvBuilder = _serviceCollection.AddControllers(options =>
_serviceCollection.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
_serviceCollection.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
var mvcBuilder = _serviceCollection.AddControllers(options =>
{
options.InputFormatters.Add(new YamlInputFormatter());
options.OutputFormatters.Add(new YamlOutputFormatter());
options.FormatterMappings.SetMediaTypeMappingForFormat("yaml", CustomMediaTypes.ApplicationYaml);
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
})
.AddJsonOptions(options => ConfigureJsonOptions(options.JsonSerializerOptions))
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = WriteValidationErrorResponse;
Expand All @@ -206,12 +217,12 @@ internal void ConfigureWebServices()
{
_logger.LogDebug("Configured controller {PluginServices} order {Order}", pluginKeyPair.Value.GetType().Name,
pluginKeyPair.Key);
pluginKeyPair.Value.ConfigureControllers(mcvBuilder);
pluginKeyPair.Value.ConfigureControllers(mvcBuilder);
});
_serviceCollection.Configure<ForwardedHeadersOptions>(options => options.ForwardedHeaders = ForwardedHeaders.All);
}

internal void ConfigureDefaultServices()
private void ConfigureDefaultServices()
{
var logLevel = _configuration.GetValue<LogEventLevel?>("serilog:minimumLevel") ?? LogEventLevel.Information;
_serviceCollection.WithLogLevelSwitch(logLevel);
Expand All @@ -231,14 +242,44 @@ internal void ConfigureDefaultServices()
_serviceCollection.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();

_serviceCollection.Configure<JsonOptions>(options =>
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));
ConfigureJsonOptions(options.SerializerOptions));

foreach (var configuration in _implementationResolver.FindAndResolve(f => f.HasAttribute<ConfigurationAttribute>()))
{
_serviceCollection.AddSingleton(configuration!.GetType(), configuration!);
}
}

private static void ConfigureJsonOptions(JsonSerializerOptions options)
{
var defaultOpts = SerializationExtensions.DefaultOptions;
options.AllowTrailingCommas = defaultOpts.AllowTrailingCommas;
options.DefaultBufferSize = defaultOpts.DefaultBufferSize;
options.IgnoreReadOnlyFields = defaultOpts.IgnoreReadOnlyFields;
options.IgnoreReadOnlyProperties = defaultOpts.IgnoreReadOnlyProperties;
options.DefaultIgnoreCondition = defaultOpts.DefaultIgnoreCondition;
options.DictionaryKeyPolicy = defaultOpts.DictionaryKeyPolicy;
options.Encoder = defaultOpts.Encoder;
options.IncludeFields = defaultOpts.IncludeFields;
options.MaxDepth = defaultOpts.MaxDepth;
options.NumberHandling = defaultOpts.NumberHandling;
options.PreferredObjectCreationHandling = defaultOpts.PreferredObjectCreationHandling;
options.PropertyNameCaseInsensitive = defaultOpts.PropertyNameCaseInsensitive;
options.PropertyNamingPolicy = defaultOpts.PropertyNamingPolicy;
options.ReadCommentHandling = defaultOpts.ReadCommentHandling;
options.ReferenceHandler = defaultOpts.ReferenceHandler;
options.TypeInfoResolver = defaultOpts.TypeInfoResolver;
options.UnknownTypeHandling = defaultOpts.UnknownTypeHandling;
options.UnmappedMemberHandling = defaultOpts.UnmappedMemberHandling;
options.WriteIndented = defaultOpts.WriteIndented;
options.TypeInfoResolverChain.Clear();
options.Converters.Clear();
foreach (var converter in defaultOpts.Converters)
options.Converters.Add(converter);
foreach (var typeInfoResolver in defaultOpts.TypeInfoResolverChain)
options.TypeInfoResolverChain.Add(typeInfoResolver);
}

internal static IActionResult WriteValidationErrorResponse(ActionContext actionContext)
{
var fieldValidationErrors = actionContext.ModelState
Expand Down

0 comments on commit 37b5bbd

Please sign in to comment.