Skip to content

feat(event-handler): add route management system for ApiGw event handler #4211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 25, 2025

Conversation

svozza
Copy link
Contributor

@svozza svozza commented Jul 24, 2025

Summary

Adds the route management layer for the API Gateway event handler. The main focus of this PR is to create the registry that will hold all the handlers registered to various routes and integrate it with the BaseRouter class added in #3972. Route validation functions were also added.

Changes

  • Added a Route class for internally representing routes
  • Added the RouteHandlerRegistry class, following the pattern established in the appsync-events and appsync-resolver event handlers.
    • The registry exposes a register function where handlers are registered and stored by route id (HttpMethod:path) and by HTTP method
    • Handles duplicate added routes in a last one wins fashion
    • Validates routes and logs a warning for invalid paths and skips adding them
    • Provides methods to query the registry for size, routes and routes by HTTP method
  • Updated the BaseRouter class:
    • route method is no longer abstract and registration of routes using the RouteHandlerRegistry class is done in one place here
    • Removed unnecessary third argument for HTTP method functions
  • Added utilities for working with paths:
    • I have chosen not add these as static methods in a class as suggested in Feature request: Implement Route Management System for Event Handler #4139. Instead, I have create a utility module with the new functions. My reasoning is that these are pure functions that do not require the incidental complexity of a class. As the experience with the EnvService has shown, should we wish to use these elsewhere in the future, it is easier to use plain functions rather than require consumers to instantiate a class with multiple methods when all they might want is one function.
    • This module defines a strict regex to define what form parameters in a path (e.g., /user/:userId) can take.
      • The first character of a parameter must be a colon
      • A parameter can only contain one colon
      • The first character after the colon must be a letter or an underscore
      • Characters after the first letter must be letters, numbers or underscores
      • Multiple parameters are permitted, e.g., /:param1/:param2/:param3
    • The validatePathPattern function ensures that parameters conform to the rules described above
    • The compilePath function is responsible for converting paths into regexes that will then be used when implementing Feature request: Implement Route Matching & Resolution System for Event Handler #4140
  • Added tests for all newly added classes and updated the BaseRouter tests to reflect the changes made there.

Issue number: closes #4139


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@svozza svozza requested a review from dreamorosi July 24, 2025 16:38
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Jul 24, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Jul 24, 2025
@dreamorosi
Copy link
Contributor

Thank you Stefano, I'll block some time to review this for tomorrow.

@svozza
Copy link
Contributor Author

svozza commented Jul 24, 2025

Sounds good. I've hewn fairly close to your sample implementation, aside from the utility stuff, so hopefully it won't be too much work.

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work on this PR!

Besides some style/organization comments, I left two more interesting comments

  • when we log issues, should we do so in separate log entries (current) or in a single one?
  • should we store routes in the registry as a set rather than an array?

@svozza svozza force-pushed the rest-handler-route-registry branch from 1f3befa to 7c2c347 Compare July 25, 2025 14:24
dreamorosi
dreamorosi previously approved these changes Jul 25, 2025
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work on the PR and for addressing my previous comments.

I've answered your questions about logs & Set usage and I don't feel strongly about either at this stage, so I'm happy for you to make a decision and either resolve the conversations, then merge - or change it + me sending another "Approve" right after that.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @svozza @dreamorosi I just left one comment. Pls let me know if make sense.

Copy link

@svozza svozza merged commit c2cbb64 into aws-powertools:main Jul 25, 2025
46 checks passed
@svozza svozza deleted the rest-handler-route-registry branch July 25, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event-handler This item relates to the Event Handler Utility size/XL PRs between 500-999 LOC, often PRs that grown with feedback tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Implement Route Management System for Event Handler
3 participants