| | | 1 | | /******************************************************************************** |
| | | 2 | | * ApiGatewayHttpApiV2Router.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Net; |
| | | 9 | | using System.Net.Http; |
| | | 10 | | using System.Text.Json; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | |
| | | 14 | | using Amazon.Lambda.APIGatewayEvents; |
| | | 15 | | |
| | | 16 | | namespace NanoRoute.AwsLambda |
| | | 17 | | { |
| | | 18 | | using Json; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Routes API Gateway HTTP API and Lambda Function URL <see cref="APIGatewayHttpApiV2ProxyRequest"/> instances thro |
| | | 22 | | /// </summary> |
| | | 23 | | public sealed class ApiGatewayHttpApiV2Router : Router |
| | | 24 | | { |
| | 1 | 25 | | private ApiGatewayHttpApiV2Router(RouterBuilder<ApiGatewayHttpApiV2Router, AwsLambdaRouterConfig> builder) : bas |
| | 1 | 26 | | { |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Routes an API Gateway HTTP API or Lambda Function URL payload-format-2.0 request and returns the correspondi |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="request">The API Gateway v2 request event.</param> |
| | | 33 | | /// <param name="services">The service provider exposed to handlers through <see cref="RequestContext.Services"/ |
| | | 34 | | /// <param name="remainingTime">The time left before the Lambda invocation times out. Expired execution produces |
| | | 35 | | /// <returns>The API Gateway v2 proxy response generated by NanoRoute.</returns> |
| | | 36 | | public async Task<APIGatewayHttpApiV2ProxyResponse> Route(APIGatewayHttpApiV2ProxyRequest request, IServiceProvi |
| | | 37 | | { |
| | | 38 | | Ensure.NotNull(request, nameof(request)); |
| | | 39 | | Ensure.NotNull(services, nameof(services)); |
| | | 40 | | |
| | | 41 | | TimeSpan cancellationDelay = remainingTime.Add(TimeSpan.FromSeconds(-1)); |
| | | 42 | | if (cancellationDelay <= TimeSpan.Zero) |
| | | 43 | | return Timeout(); |
| | | 44 | | |
| | | 45 | | using CancellationTokenSource cts = new(); |
| | | 46 | | cts.CancelAfter(cancellationDelay); |
| | | 47 | | |
| | | 48 | | using HttpRequestMessage requestMessage = request.CreateRequestMessage(); |
| | | 49 | | |
| | | 50 | | try |
| | | 51 | | { |
| | | 52 | | using HttpResponseMessage responseMessage = await Handle(requestMessage, services, cts.Token); |
| | | 53 | | |
| | | 54 | | return await responseMessage.CreateResponse(); |
| | | 55 | | } |
| | | 56 | | catch (OperationCanceledException) |
| | | 57 | | { |
| | | 58 | | return Timeout(); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | APIGatewayHttpApiV2ProxyResponse Timeout() => new() |
| | | 62 | | { |
| | | 63 | | StatusCode = (int) HttpStatusCode.GatewayTimeout, |
| | | 64 | | Body = JsonSerializer.Serialize |
| | | 65 | | ( |
| | | 66 | | new ErrorDetails |
| | | 67 | | { |
| | | 68 | | Status = HttpStatusCode.GatewayTimeout, |
| | | 69 | | Title = "Gateway Timeout", |
| | | 70 | | TraceId = request.RequestContext.RequestId |
| | | 71 | | }, |
| | | 72 | | ErrorDetails.JsonTypeInfo |
| | | 73 | | ), |
| | | 74 | | Headers = new Dictionary<string, string> |
| | | 75 | | { |
| | | 76 | | ["Content-Type"] = "application/json" |
| | | 77 | | } |
| | | 78 | | }; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Creates a strongly typed builder for configuring an <see cref="ApiGatewayHttpApiV2Router"/>. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns>A builder that can register handlers, value parsers, and router configuration.</returns> |
| | 1 | 85 | | public static RouterBuilder<ApiGatewayHttpApiV2Router, AwsLambdaRouterConfig> CreateBuilder() => new RouterBuild |
| | | 86 | | } |
| | | 87 | | } |