// handleWithGlobalMiddlwares wraps the handler function for a request with // the server's global middlewares. The order of the middlewares is backwards, // meaning that the first in the list will be evaluated last. func (s *Server) handleWithGlobalMiddlewares(handler httputils.APIFunc) httputils.APIFunc { next := handler handleVersion := middleware.NewVersionMiddleware(dockerversion.Version, api.DefaultVersion, api.MinVersion) next = handleVersion(next) if s.cfg.EnableCors { handleCORS := middleware.NewCORSMiddleware(s.cfg.CorsHeaders) next = handleCORS(next) } handleUserAgent := middleware.NewUserAgentMiddleware(s.cfg.Version) next = handleUserAgent(next) // Only want this on debug level if s.cfg.Logging && logrus.GetLevel() == logrus.DebugLevel { next = middleware.DebugRequestMiddleware(next) } if len(s.cfg.AuthorizationPluginNames) > 0 { s.authZPlugins = authorization.NewPlugins(s.cfg.AuthorizationPluginNames) handleAuthorization := middleware.NewAuthorizationMiddleware(s.authZPlugins) next = handleAuthorization(next) } return next }
func (cli *DaemonCli) initMiddlewares(s *apiserver.Server, cfg *apiserver.Config) { v := cfg.Version vm := middleware.NewVersionMiddleware(v, api.DefaultVersion, api.MinVersion) s.UseMiddleware(vm) if cfg.EnableCors { c := middleware.NewCORSMiddleware(cfg.CorsHeaders) s.UseMiddleware(c) } u := middleware.NewUserAgentMiddleware(v) s.UseMiddleware(u) if len(cli.Config.AuthorizationPlugins) > 0 { authZPlugins := authorization.NewPlugins(cli.Config.AuthorizationPlugins) handleAuthorization := authorization.NewMiddleware(authZPlugins) s.UseMiddleware(handleAuthorization) } }
// handleWithGlobalMiddlwares wraps the handler function for a request with // the server's global middlewares. The order of the middlewares is backwards, // meaning that the first in the list will be evaluated last. // // Example: handleWithGlobalMiddlewares(s.getContainersName) // // s.loggingMiddleware( // s.userAgentMiddleware( // s.corsMiddleware( // versionMiddleware(s.getContainersName) // ) // ) // ) // ) func (s *Server) handleWithGlobalMiddlewares(handler httputils.APIFunc) httputils.APIFunc { middlewares := []middleware{ versionMiddleware, s.corsMiddleware, s.userAgentMiddleware, } // Only want this on debug level if s.cfg.Logging && logrus.GetLevel() == logrus.DebugLevel { middlewares = append(middlewares, debugRequestMiddleware) } if len(s.cfg.AuthorizationPluginNames) > 0 { s.authZPlugins = authorization.NewPlugins(s.cfg.AuthorizationPluginNames) middlewares = append(middlewares, s.authorizationMiddleware) } h := handler for _, m := range middlewares { h = m(h) } return h }