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 all commits
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
@@ -0,0 +1,20 @@
namespace Amazon.Lambda.AspNetCoreServer.Hosting
{
/// <summary>
/// Options for configuring AWS Lambda content type transformation
/// </summary>
public class EncodingOptions : IEncodingOptions
{
/// <summary>
/// Defines a mapping from registered content types to the response encoding format
/// which dictates what transformations should be applied before returning response content
/// </summary>
public Dictionary<string, ResponseContentEncoding>? ResponseContentEncodingForContentType { get; set ; }

/// <summary>
/// Defines a mapping from registered content encodings to the response encoding format
/// which dictates what transformations should be applied before returning response content
/// </summary>
public Dictionary<string, ResponseContentEncoding>? ResponseContentEncodingForContentEncoding { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ public class HostingOptions
/// back to JSON to return to Lambda.
/// </summary>
public ILambdaSerializer Serializer { get; set; }

/// <summary>
/// The encoding response options sent from the Lambda to the API Gateway.
/// Used to adjust what content types are sent to the API Gatway as binary data
/// </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
@@ -0,0 +1,8 @@
namespace Amazon.Lambda.AspNetCoreServer.Hosting
{
public interface IEncodingOptions
{
public Dictionary<string, ResponseContentEncoding>? ResponseContentEncodingForContentType { get; set; }
public Dictionary<string, ResponseContentEncoding>? ResponseContentEncodingForContentEncoding { get; set; }
}
}
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 All @@ -51,6 +51,34 @@ public override Task StartAsync<TContext>(IHttpApplication<TContext> application
/// <param name="serviceProvider"></param>
/// <returns></returns>
protected abstract HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider);

/// <summary>
/// Registers encoding options for the given <see cref="AbstractAspNetCoreFunction{TREQUEST, TRESPONSE}"/>
/// </summary>
/// <typeparam name="TREQUEST"></typeparam>
/// <typeparam name="TRESPONSE"></typeparam>
/// <param name="aspNetCoreFunction"></param>
protected void RegisterEncodingOptions<TREQUEST, TRESPONSE>(AbstractAspNetCoreFunction<TREQUEST, TRESPONSE> aspNetCoreFunction)
{
if (EncodingOptions != null)
{
if (EncodingOptions.ResponseContentEncodingForContentType != null)
{
foreach (var responseContentEncodingForContentType in EncodingOptions.ResponseContentEncodingForContentType)
{
aspNetCoreFunction.RegisterResponseContentEncodingForContentType(responseContentEncodingForContentType.Key, responseContentEncodingForContentType.Value);
}
}

if (EncodingOptions.ResponseContentEncodingForContentEncoding != null)
{
foreach (var responseContentEncodingForContentEncoding in EncodingOptions.ResponseContentEncodingForContentEncoding)
{
aspNetCoreFunction.RegisterResponseContentEncodingForContentEncoding(responseContentEncodingForContentEncoding.Key, responseContentEncodingForContentEncoding.Value);
}
}
}
}
}

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

Expand Down Expand Up @@ -115,7 +145,9 @@ public APIGatewayRestApiLambdaRuntimeSupportServer(IServiceProvider serviceProvi
/// <returns></returns>
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new APIGatewayRestApiMinimalApi(serviceProvider).FunctionHandlerAsync;
var apiGatewayRestApiMinimalApi = new APIGatewayRestApiMinimalApi(serviceProvider);
RegisterEncodingOptions(apiGatewayRestApiMinimalApi);
var handler = apiGatewayRestApiMinimalApi.FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer);
}

Expand Down Expand Up @@ -156,7 +188,9 @@ public ApplicationLoadBalancerLambdaRuntimeSupportServer(IServiceProvider servic
/// <returns></returns>
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new ApplicationLoadBalancerMinimalApi(serviceProvider).FunctionHandlerAsync;
var applicationLoadBalancerMinimalApi = new ApplicationLoadBalancerMinimalApi(serviceProvider);
RegisterEncodingOptions(applicationLoadBalancerMinimalApi);
var handler = applicationLoadBalancerMinimalApi.FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer);
}

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