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

Middleware per handler #133 #149

Merged
merged 27 commits into from
Oct 22, 2019
Merged

Middleware per handler #133 #149

merged 27 commits into from
Oct 22, 2019

Conversation

0michalsokolowski0
Copy link
Contributor

  • Implemented Middleware per handler
  • Added tests

message/router.go Outdated Show resolved Hide resolved
message/router.go Outdated Show resolved Hide resolved
// first added middlewares should be executed first (so should be at the top of call stack)
for i := len(middlewares) - 1; i >= 0; i-- {
middlewareHandler = middlewares[i](middlewareHandler)
for i := 0; i < len(middlewares); i++ {
Copy link
Member

Choose a reason for hiding this comment

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

so as we discussed - it should be done like before ;) (middlewares are applied in reversed order because the "outside" middleware is called first)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right I will remove the change.

@m110
Copy link
Member

m110 commented Oct 8, 2019

The new API can be added to the docs as well. :) Probably the best place is https://watermill.io/docs/messages-router/#middleware

message/router.go Outdated Show resolved Hide resolved
message/router.go Outdated Show resolved Hide resolved
message/router.go Outdated Show resolved Hide resolved
message/router.go Outdated Show resolved Hide resolved
Michal Sokolowski added 3 commits October 8, 2019 23:29
@@ -397,19 +423,24 @@ type handler struct {
messagesCh <-chan *Message

closeCh chan struct{}

router *Router
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this one is no longer needed.

handler *handler
}

func NewHandler(router *Router, handler *handler) *Handler {
Copy link
Member

Choose a reason for hiding this comment

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

it probably may be private ;)

Copy link
Member

Choose a reason for hiding this comment

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

or even we can create it without constructor (it's used only in the package scope once, so it's fine)

IsRouterLevel bool
}

func newMiddleware(handler HandlerMiddleware, handlerName string, isRouterLevel bool) middleware {
Copy link
Member

Choose a reason for hiding this comment

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

here also - we can live without constructor ;)

r.addMiddleware("", true, m...)
}

func (r *Router) addMiddleware(handlerName string, isRouterLevel bool, m ...HandlerMiddleware) {
Copy link
Member

Choose a reason for hiding this comment

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

in general IMO it's a good idea to avoid any bool in arguments and create separated functions here (and as I understand we need to pass handlerName only if it's handler level middleware, so it may also simplify it a bit

but in general, I like the design to keep everything in one slice :)

@roblaszczak
Copy link
Member

roblaszczak commented Oct 12, 2019

@0michalsokolowski0 I added a couple of comments, but I like the current API, what do you think @m110?

It would be also nice to add some documentation of how to use it :) (you need to find in the docs where it's already mentioned how to use middlewares)

@m110
Copy link
Member

m110 commented Oct 12, 2019

Yeah I think the API is looking good now, I would maybe consider if we can come up with better names than handler and Handler. And docs would be nice 👍

@0michalsokolowski0
Copy link
Contributor Author

@roblaszczak @m110 Please correct me if I am wrong but it seems to me that currently examples can only be updated after a release. So if I would like to extend _examples/basic/3-router I can only do it after a release, since the example's dependencies point to specific version not the current implementation.

@m110
Copy link
Member

m110 commented Oct 14, 2019

Yes, that's true. One way would be to add a draft PR, with go.mod pointing to this branch and after release just bump the version there.

@roblaszczak roblaszczak merged commit 9ce4135 into ThreeDotsLabs:master Oct 22, 2019
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 this pull request may close these issues.

None yet

4 participants