| | | 1 | | /******************************************************************************** |
| | | 2 | | * ApiGatewayV2Router.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 | | /// <summary> |
| | | 19 | | /// Routes API Gateway HTTP API and Lambda Function URL <see cref="APIGatewayHttpApiV2ProxyRequest"/> instances thro |
| | | 20 | | /// </summary> |
| | | 21 | | /// <example> |
| | | 22 | | /// <code> |
| | | 23 | | /// ApiGatewayV2Router router = ApiGatewayV2Router |
| | | 24 | | /// .CreateBuilder() |
| | | 25 | | /// .AddJsonErrorDetails() |
| | | 26 | | /// .AddDefaultValueParsers() |
| | | 27 | | /// .AddHandler("GET", "/health/", (context, _) => HttpResponseMessage.Json(new { status = "ok" })) |
| | | 28 | | /// .CreateRouter(); |
| | | 29 | | /// </code> |
| | | 30 | | /// </example> |
| | | 31 | | public sealed class ApiGatewayV2Router : Router<ApiGatewayV2Router, ApiGatewayV2RouterConfig> |
| | | 32 | | { |
| | 1 | 33 | | private ApiGatewayV2Router(RouterBuilder<ApiGatewayV2Router, ApiGatewayV2RouterConfig> builder) : base(builder) |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Routes an API Gateway HTTP API or Lambda Function URL payload-format-2.0 request and returns the correspondi |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="request">The API Gateway v2 request event.</param> |
| | | 39 | | /// <param name="services">The service provider exposed to handlers through <see cref="RequestContext.Services"/ |
| | | 40 | | /// <param name="remainingTime">The time left before the Lambda invocation times out. Expired execution produces |
| | | 41 | | /// <returns>The API Gateway v2 proxy response generated by NanoRoute.</returns> |
| | | 42 | | /// <exception cref="ArgumentNullException"> |
| | | 43 | | /// Thrown when <paramref name="request"/> or <paramref name="services"/> is <see langword="null"/>. |
| | | 44 | | /// </exception> |
| | | 45 | | /// <exception cref="ArgumentException">Thrown when the request uses an unsupported HTTP method.</exception> |
| | | 46 | | /// <exception cref="InvalidOperationException">Thrown when the API Gateway request does not contain a recogniza |
| | | 47 | | /// <exception cref="HttpRequestException"> |
| | | 48 | | /// Thrown when no handler matches the request path or a matched handler signals an HTTP failure that is not |
| | | 49 | | /// translated by middleware. |
| | | 50 | | /// </exception> |
| | | 51 | | /// <example> |
| | | 52 | | /// <code> |
| | | 53 | | /// APIGatewayHttpApiV2ProxyResponse response = await router.Route(request, services, context.RemainingTime); |
| | | 54 | | /// </code> |
| | | 55 | | /// </example> |
| | | 56 | | public async Task<APIGatewayHttpApiV2ProxyResponse> Route(APIGatewayHttpApiV2ProxyRequest request, IServiceProvi |
| | | 57 | | { |
| | | 58 | | ArgumentNullException.ThrowIfNull(request); |
| | | 59 | | ArgumentNullException.ThrowIfNull(services); |
| | | 60 | | |
| | | 61 | | TimeSpan cancellationDelay = remainingTime - Config.LambdaTimeoutBuffer; |
| | | 62 | | if (cancellationDelay <= TimeSpan.Zero) |
| | | 63 | | return Timeout(); |
| | | 64 | | |
| | | 65 | | using CancellationTokenSource cts = new(); |
| | | 66 | | cts.CancelAfter(cancellationDelay); |
| | | 67 | | |
| | | 68 | | using HttpRequestMessage requestMessage = request.CreateRequestMessage(); |
| | | 69 | | |
| | | 70 | | try |
| | | 71 | | { |
| | | 72 | | using HttpResponseMessage responseMessage = await Handle(requestMessage, services, cts.Token).ConfigureA |
| | | 73 | | |
| | | 74 | | return await responseMessage.CreateResponse().ConfigureAwait(false); |
| | | 75 | | } |
| | | 76 | | catch (OperationCanceledException) |
| | | 77 | | { |
| | | 78 | | return Timeout(); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | APIGatewayHttpApiV2ProxyResponse Timeout() => new() |
| | | 82 | | { |
| | | 83 | | StatusCode = (int) HttpStatusCode.GatewayTimeout, |
| | | 84 | | Body = JsonSerializer.Serialize |
| | | 85 | | ( |
| | | 86 | | new ErrorDetails |
| | | 87 | | { |
| | | 88 | | Status = HttpStatusCode.GatewayTimeout, |
| | | 89 | | Title = "Gateway Timeout", |
| | | 90 | | TraceId = request.RequestContext.RequestId |
| | | 91 | | }, |
| | | 92 | | ErrorDetails.JsonTypeInfo |
| | | 93 | | ), |
| | | 94 | | Headers = new Dictionary<string, string> |
| | | 95 | | { |
| | | 96 | | ["Content-Type"] = "application/json" |
| | | 97 | | } |
| | | 98 | | }; |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |