Skip to content

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

@flimzy

Description

@flimzy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions