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

Move middleware to interfaces. #21897

Merged
merged 1 commit into from Apr 12, 2016

Conversation

calavera
Copy link
Contributor

@calavera calavera commented Apr 8, 2016

This makes separating middlewares from the core api easier.
As an example, the authorization middleware is moved to
it's own package.

Initialize all static middlewares when the server is created, reducing
allocations every time a route is wrapper with the middlewares.

Signed-off-by: David Calavera david.calavera@gmail.com

@thaJeztah
Copy link
Member

ping @vdemeester @MHBauer PTAL

// Middleware is an interface to allow the use of ordinary functions as Docker API filters.
// Any struct that has the appropriate signature can be registered as a middleware.
type Middleware interface {
WrapHandler(func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error
Copy link
Member

Choose a reason for hiding this comment

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

We could still use httputils.APIFunc here.

type MiddleWare interface {
    WrapHandler(httputils.APIFunc) httputils.APIFunc
}

@calavera Is there a reason why not ?

Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid references to /pkg. There is a script that checks for that. Perhaps the APIFunc definition, or the whole httputils should move to pkg as well.

Copy link
Member

Choose a reason for hiding this comment

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

Naive question : why avoiding to reference /pkg ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I misspoke above. I meant "To avoid references from pkg".

There is a build script validate-pkg that exists to prevent references from pkg to internal docker stuff. The thought is that externals pkg should stand alone.

I might be over thinking this. No ☕ this morning.

Copy link
Contributor

Choose a reason for hiding this comment

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

And I'm repeating myself, great.

Copy link
Member

Choose a reason for hiding this comment

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

middleware is in api/server/middleware and httputils is in api/server/httputils so.. there is real pkg stuff in here 👼
/me wants a bit of coffee too 😹

Copy link
Member

Choose a reason for hiding this comment

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

No idea what this conversation is about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no idea either. I'd actually rather remove the APIFunc type, but I don't want to do it here/yet.

Copy link
Member

Choose a reason for hiding this comment

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

@calavera @cpuguy83 I was just wondering why APIFunc was removed 😝

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be some type, it's unreadable without :( Or maybe combine variables to one struct, like a trend in modern frameworks.

@MHBauer
Copy link
Contributor

MHBauer commented Apr 11, 2016

Does APIFunc or httputils need to become a pkg/?

}

// WrapHandler returns a new handler function wrapping the previous one in the request chain.
func (m Middleware) WrapHandler(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

This would need to be an APIFunc, but it's in pkg, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right

@cpuguy83
Copy link
Member

SGTM moving to code review

if s.cfg.EnableCors {
handleCORS := middleware.NewCORSMiddleware(s.cfg.CorsHeaders)
next = handleCORS(next)
if len(s.middlewares) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check the length 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.

removed the check.

This makes separating middlewares from the core api easier.
As an example, the authorization middleware is moved to
it's own package.

Initialize all static middlewares when the server is created, reducing
allocations every time a route is wrapper with the middlewares.

Signed-off-by: David Calavera <david.calavera@gmail.com>
@vieux
Copy link
Contributor

vieux commented Apr 12, 2016

SGTM

@cpuguy83
Copy link
Member

LGTM

1 similar comment
@LK4D4
Copy link
Contributor

LK4D4 commented Apr 12, 2016

LGTM

@LK4D4 LK4D4 merged commit 9438572 into moby:master Apr 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants