|
2 | 2 | // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
3 | 3 | // See the LICENSE file in the project root for more information |
4 | 4 |
|
| 5 | +using System.Text.Json.Serialization; |
| 6 | +using Amazon.Lambda.APIGatewayEvents; |
| 7 | +using Amazon.Lambda.Serialization.SystemTextJson; |
| 8 | +using Elastic.Documentation.Api.Core.AskAi; |
| 9 | +using Elastic.Documentation.Api.Core.Search; |
5 | 10 | using Elastic.Documentation.Api.Infrastructure; |
6 | 11 | using Elastic.Documentation.ServiceDefaults; |
7 | | -using Microsoft.Extensions.Logging; |
8 | 12 | using OpenTelemetry; |
9 | 13 | using OpenTelemetry.Metrics; |
10 | 14 | using OpenTelemetry.Trace; |
|
48 | 52 | process.Refresh(); |
49 | 53 | Console.WriteLine($"Elastic OTel configured. Memory: {process.WorkingSet64 / 1024 / 1024} MB"); |
50 | 54 |
|
51 | | - // Configure Kestrel to listen on port 8080 for Lambda Web Adapter |
52 | | - // Lambda Web Adapter expects the app to run as a standard HTTP server on localhost:8080 |
53 | | - _ = builder.WebHost.ConfigureKestrel(serverOptions => |
| 55 | + // If we are running in Lambda Web Adapter response_stream mode, configure Kestrel to listen on port 8080 |
| 56 | + // Otherwise, configure AWS Lambda hosting for API Gateway HTTP API |
| 57 | + if (Environment.GetEnvironmentVariable("AWS_LWA_INVOKE_MODE") == "response_stream") |
54 | 58 | { |
55 | | - serverOptions.ListenLocalhost(8080); |
56 | | - }); |
| 59 | + // Configure Kestrel to listen on port 8080 for Lambda Web Adapter |
| 60 | + // Lambda Web Adapter expects the app to run as a standard HTTP server on localhost:8080 |
| 61 | + _ = builder.WebHost.ConfigureKestrel(serverOptions => |
| 62 | + { |
| 63 | + serverOptions.ListenLocalhost(8080); |
| 64 | + }); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + // Configure AWS Lambda hosting with custom JSON serializer context for API Gateway HTTP API |
| 69 | + _ = builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi, new SourceGeneratorLambdaJsonSerializer<LambdaJsonSerializerContext>()); |
| 70 | + _ = builder.WebHost.UseKestrelHttpsConfiguration(); |
| 71 | + } |
| 72 | + |
57 | 73 | process.Refresh(); |
58 | 74 | Console.WriteLine($"Kestrel configured to listen on port 8080. Memory: {process.WorkingSet64 / 1024 / 1024} MB"); |
59 | 75 |
|
|
82 | 98 | Console.WriteLine($"Stack trace: {ex.StackTrace}"); |
83 | 99 | throw; |
84 | 100 | } |
| 101 | + |
| 102 | +[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyRequest))] |
| 103 | +[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyResponse))] |
| 104 | +[JsonSerializable(typeof(AskAiRequest))] |
| 105 | +[JsonSerializable(typeof(SearchRequest))] |
| 106 | +[JsonSerializable(typeof(SearchResponse))] |
| 107 | +internal sealed partial class LambdaJsonSerializerContext : JsonSerializerContext; |
0 commit comments