add Lookup and AllowedMethods methods, change custom NotFound semantics #47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
I've used something like this fork of
pat
for a while, it adds two methods that can be very useful when building middleware chains:AllowedMethods
: returns the allowed methods for a given path, useful esp. for a CORS middleware.Lookup
: returns the matching registered handler for a given method and path, useful if in a middleware you want to check if the request actually has a handler before starting setting up the usual chain (e.g. logging, panic recover, cors, etc.)I also made another change that may be a bit trickier to merge, I found it quite weird that setting a custom
NotFound
handler on the mux changed the way the 404 was returned. If there's noNotFound
and a path has other methods registered, it returns a 405, and the 404 is only returned if there's no match and no other methods for the path. Once you set a custom NotFound handler, the behaviour changes and the 405 is not returned anymore (presumably it's because the custom not found should handle it? Still seems weird/unexpected).So this PR changes the behaviour so that setting a custom
NotFound
still behaves the same way as without it - it's just called if no 405 was returned. A case could probably be made to add support for a customMethodNotAllowed
handler, but I didn't add this.I'm fine with keeping this stuff in my fork if you're not interested in adding more API surface to the package, no worries.
Thanks,
Martin