|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using Amazon.Lambda.APIGatewayEvents; |
| 6 | +using Amazon.Lambda.ApplicationLoadBalancerEvents; |
| 7 | +using Amazon.Lambda.Core; |
| 8 | +using Amazon.Lambda.RuntimeSupport; |
| 9 | +using Amazon.Lambda.Serialization.SystemTextJson; |
| 10 | +using ApplicationLifecycle; |
| 11 | +using CommandLine; |
| 12 | + |
| 13 | +namespace AspNetCoreWebApiLambdaApplication |
| 14 | +{ |
| 15 | + internal class Program |
| 16 | + { |
| 17 | + private class Options |
| 18 | + { |
| 19 | + [Option("handler", Required = true, HelpText = "Handler function to use.")] |
| 20 | + public string Handler { get; set; } |
| 21 | + } |
| 22 | + |
| 23 | + private static string _port = ""; |
| 24 | + private static string _handlerToInvoke = ""; |
| 25 | + |
| 26 | + private static void Main(string[] args) |
| 27 | + { |
| 28 | + _port = AppLifecycleManager.GetPortFromArgs(args); |
| 29 | + |
| 30 | + _handlerToInvoke = GetHandlerFromArgs(args); |
| 31 | + |
| 32 | + using var cancellationTokenSource = new CancellationTokenSource(); |
| 33 | + using var handlerWrapper = GetHandlerWrapper(); |
| 34 | + |
| 35 | + // Instantiate a LambdaBootstrap and run it. |
| 36 | + // It will wait for invocations from AWS Lambda and call the handler function for each one. |
| 37 | + using var bootstrap = new LambdaBootstrap(handlerWrapper); |
| 38 | + |
| 39 | + _ = bootstrap.RunAsync(cancellationTokenSource.Token); |
| 40 | + |
| 41 | + AppLifecycleManager.CreatePidFile(); |
| 42 | + |
| 43 | + AppLifecycleManager.WaitForTestCompletion(_port); |
| 44 | + |
| 45 | + cancellationTokenSource.Cancel(); |
| 46 | + } |
| 47 | + |
| 48 | + private static string GetHandlerFromArgs(string[] args) |
| 49 | + { |
| 50 | + var handler = string.Empty; |
| 51 | + |
| 52 | + var commandLine = string.Join(" ", args); |
| 53 | + |
| 54 | + new Parser(with => { with.IgnoreUnknownArguments = true; }) |
| 55 | + .ParseArguments<Options>(args) |
| 56 | + .WithParsed(o => |
| 57 | + { |
| 58 | + handler = o.Handler; |
| 59 | + }); |
| 60 | + |
| 61 | + if (string.IsNullOrEmpty(handler)) |
| 62 | + throw new Exception("--handler commandline argument could not be parsed."); |
| 63 | + |
| 64 | + return handler; |
| 65 | + } |
| 66 | + |
| 67 | + private static HandlerWrapper GetHandlerWrapper() |
| 68 | + { |
| 69 | + var defaultLambdaJsonSerializer = new DefaultLambdaJsonSerializer(); |
| 70 | + |
| 71 | + switch (_handlerToInvoke) |
| 72 | + { |
| 73 | + case "APIGatewayProxyFunctionEntryPoint": |
| 74 | + { |
| 75 | + var entryPoint = new APIGatewayProxyFunctionEntryPoint(); |
| 76 | + Func<APIGatewayProxyRequest, ILambdaContext, Task<APIGatewayProxyResponse>> handlerFunc = entryPoint.FunctionHandlerAsync; |
| 77 | + |
| 78 | + return HandlerWrapper.GetHandlerWrapper(handlerFunc, defaultLambdaJsonSerializer); |
| 79 | + } |
| 80 | + case "ApplicationLoadBalancerFunctionEntryPoint": |
| 81 | + { |
| 82 | + var entryPoint = new ApplicationLoadBalancerFunctionEntryPoint(); |
| 83 | + Func<ApplicationLoadBalancerRequest, ILambdaContext, Task<ApplicationLoadBalancerResponse>> handlerFunc = entryPoint.FunctionHandlerAsync; |
| 84 | + |
| 85 | + return HandlerWrapper.GetHandlerWrapper(handlerFunc, defaultLambdaJsonSerializer); |
| 86 | + } |
| 87 | + case "APIGatewayHttpApiV2ProxyFunctionEntryPoint": |
| 88 | + { |
| 89 | + var entryPoint = new APIGatewayHttpApiV2ProxyFunctionEntryPoint(); |
| 90 | + Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handlerFunc = entryPoint.FunctionHandlerAsync; |
| 91 | + |
| 92 | + return HandlerWrapper.GetHandlerWrapper(handlerFunc, defaultLambdaJsonSerializer); |
| 93 | + } |
| 94 | + default: |
| 95 | + throw new ArgumentException($"Handler not found: {_handlerToInvoke}"); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments