Skip to content
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

Feature route metadata #760

Merged
merged 2 commits into from
Jun 19, 2024
Merged

Conversation

cornejong
Copy link

@cornejong cornejong commented May 26, 2024

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Go Version Update
  • Dependency Update

Description

Added metadata to route. as proposed in #750
Documentation will be added uppon request.

Related Tickets & Documents

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

Run verifications and test

  • make verify is passing
  • make test is passing

@cornejong cornejong changed the title added route metadata with tests Feature route metadata May 27, 2024
@cornejong
Copy link
Author

This pull request introduces metadata to route struct. The proposed changes allow attaching arbitrary metadata to routes, enabling more flexible and readable codebases.

Here's an example of how this feature can be used:

router.HandleFunc("/v1/product", handlerFunc).Metadata("authPolicy", "product.read")
// or an RBAC example
router.HandleFunc("/v1/product", handlerFunc).Metadata("allowedRoles", []Role{RoleAdmin, RoleManager, RoleEditor})

router.Use((next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        route := mux.CurrentRoute(r)
        
        if !route.MetadataContains("authPolicy") {
            next.ServeHTTP(w, r)
            return
        }

        authPolicy, err := route.GetMetadataValue("authPolicy")
        // or define a fallback value
        authPolicy := route.GetMetadataValueOr("authPolicy", "fallback_policy")
        
        // Enforce policy ...
    })
})

While authorization is a common use case, the .Metadata method opens up possibilities for various other configurations, such as caching, rate limiting or any other middleware for that matter. This addition aims to enhance the modularity and maintainability of applications by leveraging route metadata for various middleware configurations. The goal is to create clean and readable codebases where functionality can be abstracted into generalized middleware, keeping handlers clean and simple.

@AlexVulaj
Copy link
Member

This change makes a lot of sense to me, thanks for the contribution!

@AlexVulaj AlexVulaj closed this Jun 19, 2024
@AlexVulaj AlexVulaj reopened this Jun 19, 2024
@AlexVulaj AlexVulaj merged commit 10f71ea into gorilla:main Jun 19, 2024
12 of 21 checks passed
@AlexVulaj
Copy link
Member

Sorry for the "closed" notification - that was a misclick. PR is merged now!

@cornejong cornejong deleted the feature-route-meta-data branch June 20, 2024 17:15
@cornejong
Copy link
Author

cornejong commented Jun 20, 2024

No problemo, Thanks for the merge! Always happy to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Route metadata
2 participants