Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register encoding options when adding the service to the service collection #1575

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Amazon.Lambda.Core;
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.Core;

namespace Amazon.Lambda.AspNetCoreServer.Hosting;

Expand All @@ -12,4 +13,9 @@ public class HostingOptions
/// back to JSON to return to Lambda.
/// </summary>
public ILambdaSerializer Serializer { get; set; }

/// <summary>
/// The encoding response options used by the Lambda
hugohlln marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public IEncodingOptions EncodingOptions { get; set; }
hugohlln marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.DependencyInjection;

namespace Amazon.Lambda.AspNetCoreServer.Hosting.Internal
Expand All @@ -18,6 +16,7 @@ public abstract class LambdaRuntimeSupportServer : LambdaServer
{
IServiceProvider _serviceProvider;
internal ILambdaSerializer Serializer;
internal IEncodingOptions EncodingOptions;

/// <summary>
/// Creates an instance on the LambdaRuntimeSupportServer
Expand All @@ -27,6 +26,7 @@ public LambdaRuntimeSupportServer(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
Serializer = serviceProvider.GetRequiredService<ILambdaSerializer>();
EncodingOptions = serviceProvider.GetRequiredService<IEncodingOptions>();
}

/// <summary>
Expand Down Expand Up @@ -74,7 +74,7 @@ public APIGatewayHttpApiV2LambdaRuntimeSupportServer(IServiceProvider servicePro
/// <returns></returns>
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new APIGatewayHttpApiV2MinimalApi(serviceProvider).FunctionHandlerAsync;
var handler = new APIGatewayHttpApiV2MinimalApi(serviceProvider, this.EncodingOptions).FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer);
}

Expand All @@ -87,9 +87,28 @@ public class APIGatewayHttpApiV2MinimalApi : APIGatewayHttpApiV2ProxyFunction
/// Create instances
/// </summary>
/// <param name="serviceProvider">The IServiceProvider created for the ASP.NET Core application</param>
public APIGatewayHttpApiV2MinimalApi(IServiceProvider serviceProvider)
/// <param name="encodingOptions">The encoding options to register</param>
public APIGatewayHttpApiV2MinimalApi(IServiceProvider serviceProvider, IEncodingOptions? encodingOptions = null)
: base(serviceProvider)
{
if(encodingOptions != null)
hugohlln marked this conversation as resolved.
Show resolved Hide resolved
{
if (encodingOptions.ResponseContentEncodingForContentType != null)
{
foreach (var responseContentEncodingForContentType in encodingOptions.ResponseContentEncodingForContentType)
{
this.RegisterResponseContentEncodingForContentType(responseContentEncodingForContentType.Key, responseContentEncodingForContentType.Value);
}
}

if (encodingOptions.ResponseContentEncodingForContentEncoding != null)
{
foreach (var responseContentEncodingForContentEncoding in encodingOptions.ResponseContentEncodingForContentEncoding)
{
this.RegisterResponseContentEncodingForContentEncoding(responseContentEncodingForContentEncoding.Key, responseContentEncodingForContentEncoding.Value);
}
}
}
}
}
}
Expand All @@ -115,7 +134,7 @@ public APIGatewayRestApiLambdaRuntimeSupportServer(IServiceProvider serviceProvi
/// <returns></returns>
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new APIGatewayRestApiMinimalApi(serviceProvider).FunctionHandlerAsync;
var handler = new APIGatewayRestApiMinimalApi(serviceProvider, this.EncodingOptions).FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer);
}

Expand All @@ -128,9 +147,28 @@ public class APIGatewayRestApiMinimalApi : APIGatewayProxyFunction
/// Create instances
/// </summary>
/// <param name="serviceProvider">The IServiceProvider created for the ASP.NET Core application</param>
public APIGatewayRestApiMinimalApi(IServiceProvider serviceProvider)
/// /// <param name="encodingOptions">The encoding options to register</param>
public APIGatewayRestApiMinimalApi(IServiceProvider serviceProvider, IEncodingOptions? encodingOptions = null)
: base(serviceProvider)
{
if (encodingOptions != null)
{
if (encodingOptions.ResponseContentEncodingForContentType != null)
{
foreach (var responseContentEncodingForContentType in encodingOptions.ResponseContentEncodingForContentType)
{
this.RegisterResponseContentEncodingForContentType(responseContentEncodingForContentType.Key, responseContentEncodingForContentType.Value);
}
}

if (encodingOptions.ResponseContentEncodingForContentEncoding != null)
{
foreach (var responseContentEncodingForContentEncoding in encodingOptions.ResponseContentEncodingForContentEncoding)
{
this.RegisterResponseContentEncodingForContentEncoding(responseContentEncodingForContentEncoding.Key, responseContentEncodingForContentEncoding.Value);
}
}
}
}
}
}
Expand All @@ -156,7 +194,7 @@ public ApplicationLoadBalancerLambdaRuntimeSupportServer(IServiceProvider servic
/// <returns></returns>
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new ApplicationLoadBalancerMinimalApi(serviceProvider).FunctionHandlerAsync;
var handler = new ApplicationLoadBalancerMinimalApi(serviceProvider, this.EncodingOptions).FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer);
}

Expand All @@ -169,9 +207,28 @@ public class ApplicationLoadBalancerMinimalApi : ApplicationLoadBalancerFunction
/// Create instances
/// </summary>
/// <param name="serviceProvider">The IServiceProvider created for the ASP.NET Core application</param>
public ApplicationLoadBalancerMinimalApi(IServiceProvider serviceProvider)
/// <param name="encodingOptions">The encoding options to register</param>
public ApplicationLoadBalancerMinimalApi(IServiceProvider serviceProvider, IEncodingOptions? encodingOptions = null)
: base(serviceProvider)
{
if (encodingOptions != null)
{
if (encodingOptions.ResponseContentEncodingForContentType != null)
{
foreach (var responseContentEncodingForContentType in encodingOptions.ResponseContentEncodingForContentType)
{
this.RegisterResponseContentEncodingForContentType(responseContentEncodingForContentType.Key, responseContentEncodingForContentType.Value);
}
}

if (encodingOptions.ResponseContentEncodingForContentEncoding != null)
{
foreach (var responseContentEncodingForContentEncoding in encodingOptions.ResponseContentEncodingForContentEncoding)
{
this.RegisterResponseContentEncodingForContentEncoding(responseContentEncodingForContentEncoding.Key, responseContentEncodingForContentEncoding.Value);
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Amazon.Lambda.AspNetCoreServer.Hosting;
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.AspNetCoreServer.Hosting.Internal;
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -66,6 +66,7 @@ public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection ser
configure.Invoke(hostingOptions);

services.TryAddSingleton<ILambdaSerializer>(hostingOptions.Serializer ?? new DefaultLambdaJsonSerializer());
services.TryAddSingleton<IEncodingOptions>(hostingOptions.EncodingOptions);

var serverType = eventSource switch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AspNetCoreServer.Internal
hugohlln marked this conversation as resolved.
Show resolved Hide resolved
{
public class EncodingOptions : IEncodingOptions
hugohlln marked this conversation as resolved.
Show resolved Hide resolved
{
public Dictionary<string, ResponseContentEncoding> ResponseContentEncodingForContentType { get; set ; }
public Dictionary<string, ResponseContentEncoding> ResponseContentEncodingForContentEncoding { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Amazon.Lambda.AspNetCoreServer.Internal
{
public interface IEncodingOptions
{
public Dictionary<string, ResponseContentEncoding> ResponseContentEncodingForContentType { get; set; }
public Dictionary<string, ResponseContentEncoding> ResponseContentEncodingForContentEncoding { get; set; }
}
}