aws / aws/aws-lambda-dotnet

Lambda Annotations API Routing

Offen
#1,616 2 Kommentare 6 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@normj arbeitet bereits daran.

Seit 17.11.2023.

annotations feature-request p2
Vorherrschende Sprache
C#
Sterne
1.7k
Forks
503
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
21

Beschreibung

Describe the feature

With the release of the [LambdaGlobalProperties] attribute that can auto-generate the static Main() method for non managed and native AOT runtimes this functionality could be extended to provide API routing capabilities. The API annotations on methods could be used as part of a switch statement for the inbound API event.

Use Case

Running a simple web API on Lambda without needing to introduce the overhead of ASP.NET. Removes the need to set the ANNOTATIONS_HANDLER environment variable when running multiple API endpoints in the same Lambda function.

Allows the use of an API Gateway {proxy+} route with all API request routing to the same Lambda function.

Proposed Solution

The GenerateMain global properties attribute adds a switch statement based on an environment variable. Introduce a new ApiRouting Lambda Global Property that will generate the static Main method. Include a switch statement that uses a combination of the HttpMethod and Path from the incoming API event to route to the correct method.

Example of generated code:

public static async Task Main(string[] args)
{
    Func<APIGatewayProxyRequest , Task<APIGatewayProxyResponse>> apiHandler = Handler;
    await Amazon.Lambda.RuntimeSupport.LambdaBootstrapBuilder.Create(apiHandler, new Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer()).Build().RunAsync();
}

public async static Task<APIGatewayProxyResponse> Handler(APIGatewayProxyRequest request)
{
    switch ($"{request.HttpMethod.ToUpper()} {request.Path.ToUpper()}")
    {
        case "GET /products":
            return GetProductsHandler();
        default:
            return new APIGatewayProxyResponse()
            {
                StatusCode = 404
            };
    }
}

Other Information

No response

Acknowledgements
  • I may be able to implement this feature request
  • This feature might incur a breaking change
AWS .NET SDK and/or Package version used

Amazon.Lambda.Annotations

Targeted .NET Platform

.NET 6, .NET 8

Operating System and version

All

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.