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

Add wrapper for using middleware #178

Merged
merged 7 commits into from
Nov 12, 2019

Conversation

rdv821
Copy link

@rdv821 rdv821 commented Nov 4, 2019

No description provided.

Copy link

@dkukharau dkukharau left a comment

Choose a reason for hiding this comment

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

Overally good except for the minor thing.

server/server.go Outdated
Comment on lines 74 to 80
var handler http.Handler
if s.middleware != nil {
handler = s.middleware(mux)
} else {
handler = mux
}
s.HTTPServer.Handler = handler

Choose a reason for hiding this comment

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

It can probably be simplified:

	if s.middleware != nil {
		s.HTTPServer.Handler = s.middleware(s.HTTPServer.Handler)
	}

Copy link

Choose a reason for hiding this comment

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

As I mentioned in comment above, we should consider defining multiple middleware not only one.

    for _, m := range s.middleware {
         s.HTTPServer.Handler = m(s.HTTPServer.Handler)
    }

Copy link

@burov burov left a comment

Choose a reason for hiding this comment

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

I would recommend to revisit this feature

server/server.go Outdated
@@ -31,6 +31,7 @@ type Server struct {
initializers []InitializerFunc
initializeTimeout time.Duration
registrars []func(mux *http.ServeMux) error
middleware Middleware
Copy link

Choose a reason for hiding this comment

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

From my point of view field "middleware" should be repeated to allow use to define chain of middleware, solution with one middleware from my point of view isn't enough flexible

server/server.go Outdated
Comment on lines 74 to 80
var handler http.Handler
if s.middleware != nil {
handler = s.middleware(mux)
} else {
handler = mux
}
s.HTTPServer.Handler = handler
Copy link

Choose a reason for hiding this comment

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

As I mentioned in comment above, we should consider defining multiple middleware not only one.

    for _, m := range s.middleware {
         s.HTTPServer.Handler = m(s.HTTPServer.Handler)
    }

@dkukharau
Copy link

dkukharau commented Nov 4, 2019

As discussed with @burov, it's useful to add support for chaining middlewares. The preferable approach would probably be to add a utility function to be called on the client side, similar to grpc_middleware.ChainUnaryServer.

func ChainMiddlewares(middlewares ...Middleware) Middleware {
...
}

@rdv821 Could you add it to your PR?

Copy link

@dkukharau dkukharau left a comment

Choose a reason for hiding this comment

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

I'm fine with using WithMiddlewares(...). Some minor change requests though.

server/server.go Outdated
@@ -31,6 +31,7 @@ type Server struct {
initializers []InitializerFunc
initializeTimeout time.Duration
registrars []func(mux *http.ServeMux) error
middleware []Middleware

Choose a reason for hiding this comment

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

middlewares

server/server.go Outdated
@@ -67,6 +71,10 @@ func NewServer(opts ...Option) (*Server, error) {
}
s.HTTPServer.Handler = mux

for _, m := range s.middleware {

Choose a reason for hiding this comment

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

You should probably wrap them backwards, so that the order of chain execution matches the order they are provided by the client.

t.Error("Expected another status")
}

resp2, err := http.Get(fmt.Sprint("http://", httpL.Addr().String(), "/v1/hello?name=test"))

Choose a reason for hiding this comment

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

Probably it doesn't make sense to make the same request twice.

server/server.go Outdated
}

// Sort args for middleware
func (s *Server) Sort() {

Choose a reason for hiding this comment

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

Shouldn't be a public method.

server/server.go Outdated
// Sort args for middleware
func (s *Server) Sort() {
for i, j := 0, len(s.middlewares)-1; i < j; i, j = i+1, j-1 {
s.middlewares[i], s.middlewares[j] = s.middlewares[j], s.middlewares[i]

Choose a reason for hiding this comment

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

Could you also update the tests to check that the resulting order is right?

})
}

func newTestParamVerify(t *testing.T, h http.Handler) http.Handler {
Copy link
Contributor

@Evgeniy-L Evgeniy-L Nov 11, 2019

Choose a reason for hiding this comment

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

This seems not be used anywhere.

@dkukharau
Copy link

@Evgeniy-L @burov any more feedback from your side?

Copy link
Contributor

@Evgeniy-L Evgeniy-L left a comment

Choose a reason for hiding this comment

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

Looks ok

@dkukharau dkukharau merged commit 8447e15 into infobloxopen:master Nov 12, 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.

4 participants