Merge pull request #974 from stevvooe/context-cleanup

context: WithVersion and context package cleanup
This commit is contained in:
Stephen Day 2015-09-14 19:24:33 -07:00
commit 5a8fabfee3
3 changed files with 6 additions and 11 deletions

View file

@ -77,8 +77,6 @@ func NewApp(ctx context.Context, configuration *configuration.Configuration) *Ap
isCache: configuration.Proxy.RemoteURL != "", isCache: configuration.Proxy.RemoteURL != "",
} }
app.Context = ctxu.WithLogger(app.Context, ctxu.GetLogger(app, "instance.id"))
// Register the handler dispatchers. // Register the handler dispatchers.
app.register(v2.RouteNameBase, func(ctx *Context, r *http.Request) http.Handler { app.register(v2.RouteNameBase, func(ctx *Context, r *http.Request) http.Handler {
return http.HandlerFunc(apiBase) return http.HandlerFunc(apiBase)

View file

@ -35,6 +35,9 @@ var Cmd = &cobra.Command{
return return
} }
// setup context
ctx := context.WithVersion(context.Background(), version.Version)
config, err := resolveConfiguration(args) config, err := resolveConfiguration(args)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "configuration error: %v\n", err) fmt.Fprintf(os.Stderr, "configuration error: %v\n", err)
@ -51,7 +54,7 @@ var Cmd = &cobra.Command{
}(config.HTTP.Debug.Addr) }(config.HTTP.Debug.Addr)
} }
registry, err := NewRegistry(context.Background(), config) registry, err := NewRegistry(ctx, config)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -78,9 +81,6 @@ type Registry struct {
// NewRegistry creates a new registry from a context and configuration struct. // NewRegistry creates a new registry from a context and configuration struct.
func NewRegistry(ctx context.Context, config *configuration.Configuration) (*Registry, error) { func NewRegistry(ctx context.Context, config *configuration.Configuration) (*Registry, error) {
// Note this
ctx = context.WithValue(ctx, "version", version.Version)
var err error var err error
ctx, err = configureLogging(ctx, config) ctx, err = configureLogging(ctx, config)
if err != nil { if err != nil {
@ -218,7 +218,7 @@ func configureLogging(ctx context.Context, config *configuration.Configuration)
if config.Log.Level == "" && config.Log.Formatter == "" { if config.Log.Level == "" && config.Log.Formatter == "" {
// If no config for logging is set, fallback to deprecated "Loglevel". // If no config for logging is set, fallback to deprecated "Loglevel".
log.SetLevel(logLevel(config.Loglevel)) log.SetLevel(logLevel(config.Loglevel))
ctx = context.WithLogger(ctx, context.GetLogger(ctx, "version")) ctx = context.WithLogger(ctx, context.GetLogger(ctx))
return ctx, nil return ctx, nil
} }
@ -253,9 +253,6 @@ func configureLogging(ctx context.Context, config *configuration.Configuration)
log.Debugf("using %q logging formatter", config.Log.Formatter) log.Debugf("using %q logging formatter", config.Log.Formatter)
} }
// log the application version with messages
ctx = context.WithLogger(ctx, context.GetLogger(ctx, "version"))
if len(config.Log.Fields) > 0 { if len(config.Log.Fields) > 0 {
// build up the static fields, if present. // build up the static fields, if present.
var fields []interface{} var fields []interface{}

View file

@ -241,7 +241,7 @@ func (bw *blobWriter) validateBlob(ctx context.Context, desc distribution.Descri
if !verified { if !verified {
context.GetLoggerWithFields(ctx, context.GetLoggerWithFields(ctx,
map[string]interface{}{ map[interface{}]interface{}{
"canonical": canonical, "canonical": canonical,
"provided": desc.Digest, "provided": desc.Digest,
}, "canonical", "provided"). }, "canonical", "provided").