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: Expose Context.methodsAllowed for use by MethodNotAllowedHandler #870

Open
flimzy opened this issue Oct 27, 2023 · 0 comments · May be fixed by #884
Open

Feature: Expose Context.methodsAllowed for use by MethodNotAllowedHandler #870

flimzy opened this issue Oct 27, 2023 · 0 comments · May be fixed by #884

Comments

@flimzy
Copy link
Contributor

flimzy commented Oct 27, 2023

The default MethodNotAllowed handler uses Context.methodsAllowed to set the "Allow" header in the response.

It would be nice to expose that list of allowed methods to a custom handler, as well.

Options I've considered:

  1. Simply export that field of the Context struct. This would allow access when the Context value is retrieved from the r.Context()

  2. Add a function that extracts the unexported field from a context, and returns it. Something like:

    func AllowedMethods(ctx context.Context) []string {
        rctx, ok := ctx.Value(RouteCtxKey).(*Context)
        if ok {
            return rctx.methodsAllowed
        }
        return nil
    }
  3. Add a methodn on Router that takes a function that receives the list, and returns a handler. This seems really intrusive, though, and largely duplicates existing functionality:

    // CustomMethodNotAllowedHandler sets a custom MethodNotAllowed factory. Takes precidence over MethodNotAllowed if both are set
    func (mx *Mux) CustomMethodNotAllowedHandler(func(allowedMethods []string) http.HandlerFunc)
  4. Re-calculate the list somehow. I don't see an efficient way to do this, but maybe I'm overlooking something.

  5. As a last restort, I could potentially use a middeware that wraps the entire chi Mux, and detects the Allow: header to extract the values that way. Meh

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 a pull request may close this issue.

1 participant