Skip to content

feat(resources-introspection): initial support for route and HTTP method introspection #3594

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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

dertin
Copy link

@dertin dertin commented Mar 3, 2025

PR Type

Feature

PR Checklist

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt.
  • (Team) Label with affected crates and semver status.

Overview

Hello, this is my first contribution to the project. This PR introduces a new resource introspection feature for actix-web that allows retrieving all configured route paths along with their associated HTTP methods after the server has started. Currently, it supports only basic Guards that determine HTTP methods. The feature is activated using the resources-introspection feature flag.

The main method is actix_web::introspection::get_registered_resources(), which returns a vector of ResourceIntrospection objects. This method can be executed in an async task alongside the actix-web server or on a separate thread.

I developed this feature because it's extremely useful for auditing services with many route paths that change constantly. I hope it proves valuable for others as well, and I welcome any feedback or suggestions for improvement.

Closes #1462
Closes #2677

@dertin
Copy link
Author

dertin commented Mar 4, 2025

Hi, @robjtede

Here's a limited-scope approach for listing routes and methods from the Guards it’s a starting point.
Extending support to extract additional metadata might require more breaking changes
(potentially involving proc-macro / actix-web-codegen).

Please review and let me know your thoughts!

dertin added 3 commits March 5, 2025 02:34
… usage

- Added `GuardDetail` enum to encapsulate various introspection details of a guard.
- Refactored `HttpMethodsExtractor` implementation to use `GuardDetail` instead of `downcast_ref`.
… usage

- Added `GuardDetail` enum to encapsulate various introspection details of a guard.
- Refactored `HttpMethodsExtractor` implementation to use `GuardDetail` instead of `downcast_ref`.
@@ -128,6 +128,9 @@ compat = [
# Opt-out forwards-compatibility for handler visibility inheritance fix.
compat-routing-macros-force-pub = ["actix-web-codegen?/compat-routing-macros-force-pub"]

# Enabling the retrieval of metadata for initialized resources, including path and HTTP method.
resources-introspection = []
Copy link
Author

Choose a reason for hiding this comment

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

Can you suggest a better name for the feature flag? Another option could be experimental-introspection to better reflect its current state of development. Open to other naming suggestions that capture the intent of the feature.

@@ -105,6 +126,11 @@ where
let rmap = Rc::new(rmap);
ResourceMap::finish(&rmap);

#[cfg(feature = "resources-introspection")]
{
crate::introspection::process_introspection(Rc::clone(&rmap), rdef_methods);
Copy link
Author

Choose a reason for hiding this comment

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

Surely there is a better place to make the call to introspection::process_introspection. This observation applies to all locations where I am currently making this call, including scope.rs, route.rs, resource.rs, and app_service.rs. Given some time, I can improve this implementation, always aiming to minimize breaking changes. Any ideas?

@dertin dertin changed the title feat(resources-introspection): add support for resource metadata retrieval feat(resources-introspection): initial support for route and HTTP method introspection Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

way to get a list of registered services Route introspection
1 participant