Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add MethodMiddleware #366

Merged
merged 8 commits into from
May 12, 2018
Merged

Add MethodMiddleware #366

merged 8 commits into from
May 12, 2018

Conversation

fharding1
Copy link
Contributor

Implements the behavior discussed in #353 with @elithrar. I'm not sure if this is the best way to only match based on path only.

@elithrar elithrar self-assigned this Apr 24, 2018
@elithrar
Copy link
Contributor

Thanks @fharding1 - can you add tests to this as well?

@fharding1
Copy link
Contributor Author

@elithrar 👍 done.

@fharding1
Copy link
Contributor Author

bump @elithrar

@elithrar
Copy link
Contributor

Thanks for the reminder. On my list for this week!

@elithrar elithrar requested review from elithrar and kisielk April 30, 2018 23:06
@kisielk
Copy link
Contributor

kisielk commented May 1, 2018

The usage of this seems a bit confusing to me. The idea is that the router will use itself as middleware?

@fharding1
Copy link
Contributor Author

Yeah, because you might only want to apply the middleware to certain routes. I'm not super familiar with mux beyond using it in personal projects with fairly normal use cases, would there be a better way to do this that you would recommend? @kisielk

@kisielk
Copy link
Contributor

kisielk commented May 1, 2018

Since it doesn't depend on any unexported fields of the router, I don't think it needs to be a method of the router itself. It could be a new middleware function or type that takes the router as an argument. That way we don't need to grow the already large API of the Router type.

@fharding1
Copy link
Contributor Author

Hmm, I implemented it as a method on Router as that's how @elithrar recommended it in #353: "I'd be open to a PR that can do this as a feature on Router.", but I also agree with what you've said. @elithrar ?

@elithrar
Copy link
Contributor

elithrar commented May 1, 2018

With the way it’s been implemented, it doesn’t need to be on the router at all.

I had assumed it would be similar to NotFoundHandler, where it is a field on the Router, or a toggle (Boolean). I don’t feel strongly either way.

@fharding1
Copy link
Contributor Author

Alright, I've removed it from Router, the usage now looks like router.Use(MethodMiddleware(router)) which I think makes sense.

middleware.go Outdated
// on a request, by matching routes based only on paths. It also handles
// OPTIONS requests, by settings Access-Control-Allow-Methods, and then
// returning without calling the next http handler.
func MethodMiddleware(r *Router) MiddlewareFunc {
Copy link
Contributor

Choose a reason for hiding this comment

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

Two parts:

  • I think the function name could be more descriptive here: CORSMethodMiddleware - something to indicate that it applies to a CORS-driven use-case.
  • Do we want to build the routes on every request? (may not be avoidable to avoid stale data, but thinking out loud here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with the first part. As for building the routes on every request, I think it would be fairly difficult to do, but I don't know the mux architecture super well either. I can't think of any clean solution that keeps our data fresh. It would be nice to not have to walk the router on each request though of course, so I'm open to any ideas!

mux_test.go Outdated
@@ -2315,6 +2315,14 @@ func stringMapEqual(m1, m2 map[string]string) bool {
return true
}

// stringHandler returns a handler func that writes a message 's' to the http
// response writer.
Copy link
Contributor

Choose a reason for hiding this comment

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

"to the http.ResponseWriter" or just "to the response".

Copy link
Contributor

@elithrar elithrar left a comment

Choose a reason for hiding this comment

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

Minor change to the comment. Otherwise LGTM!

middleware.go Outdated

// CORSMethodMiddleware returns a MiddlewareFunc intended for setting the CORS
// Method header, Access-Control-Allow-Methods. It sets Access-Control-Allow-Methods
// on a request, by matching routes based only on paths. It also handles
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra space between "a" and "request"

middleware.go Outdated
@@ -28,3 +31,43 @@ func (r *Router) Use(mwf ...MiddlewareFunc) {
func (r *Router) useInterface(mw middleware) {
r.middlewares = append(r.middlewares, mw)
}

// CORSMethodMiddleware returns a MiddlewareFunc intended for setting the CORS
// Method header, Access-Control-Allow-Methods. It sets Access-Control-Allow-Methods
Copy link
Contributor

Choose a reason for hiding this comment

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

"Sets the Access-Control-Allow-Methods response header..."

@fharding1
Copy link
Contributor Author

Fixed! Thank you for maintaining gorilla/mux and helping me add this feature!

@elithrar elithrar merged commit 5e55a4a into gorilla:master May 12, 2018
@elithrar
Copy link
Contributor

Thanks for the contribution. Apologies for the slow responses. Right in the middle of changing jobs, so have been extremely busy!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants