1
0
mirror of https://github.com/moby/moby.git synced 2025-07-30 18:23:29 +03:00

Make server middleware standalone functions.

Removing direct dependencies from the server configuration.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera
2016-02-24 13:17:43 -05:00
parent 467c8b1a6a
commit 1ba44a832f
9 changed files with 241 additions and 184 deletions

View File

@ -0,0 +1,38 @@
package middleware
import (
"fmt"
"net/http"
"runtime"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/errors"
"github.com/docker/docker/pkg/version"
"golang.org/x/net/context"
)
// NewVersionMiddleware creates a new Version middleware.
func NewVersionMiddleware(versionCheck string, defaultVersion, minVersion version.Version) Middleware {
serverVersion := version.Version(versionCheck)
return func(handler httputils.APIFunc) httputils.APIFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
apiVersion := version.Version(vars["version"])
if apiVersion == "" {
apiVersion = defaultVersion
}
if apiVersion.GreaterThan(defaultVersion) {
return errors.ErrorCodeNewerClientVersion.WithArgs(apiVersion, defaultVersion)
}
if apiVersion.LessThan(minVersion) {
return errors.ErrorCodeOldClientVersion.WithArgs(apiVersion, minVersion)
}
header := fmt.Sprintf("Docker/%s (%s)", serverVersion, runtime.GOOS)
w.Header().Set("Server", header)
ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion)
return handler(ctx, w, r, vars)
}
}
}