From 31851edf818722cdf88746689f752109d13d00c1 Mon Sep 17 00:00:00 2001 From: HuKeping Date: Thu, 7 Apr 2016 21:02:55 +0800 Subject: [PATCH] Kind of cleanup Move the configuration parsing for notary-server to its own file. Signed-off-by: Hu Keping --- cmd/notary-server/config.go | 60 +++++++++++++++++++++++++++++++++++ cmd/notary-server/main.go | 63 ------------------------------------- 2 files changed, 60 insertions(+), 63 deletions(-) diff --git a/cmd/notary-server/config.go b/cmd/notary-server/config.go index 2ecc6aa9b2..351aabc7e8 100644 --- a/cmd/notary-server/config.go +++ b/cmd/notary-server/config.go @@ -11,6 +11,7 @@ import ( _ "github.com/docker/distribution/registry/auth/token" "github.com/docker/go-connections/tlsconfig" "github.com/docker/notary" + "github.com/docker/notary/server" "github.com/docker/notary/server/storage" "github.com/docker/notary/signer/client" "github.com/docker/notary/tuf/data" @@ -18,6 +19,7 @@ import ( "github.com/docker/notary/utils" _ "github.com/go-sql-driver/mysql" "github.com/spf13/viper" + "golang.org/x/net/context" ) // get the address for the HTTP server, and parses the optional TLS @@ -168,3 +170,61 @@ func getCacheConfig(configuration *viper.Viper) (current, consistent utils.Cache consistent = cccs[consistentOpt] return } + +func parseServerConfig(configFilePath string, hRegister healthRegister) (context.Context, server.Config, error) { + config := viper.New() + utils.SetupViper(config, envPrefix) + + // parse viper config + if err := utils.ParseViper(config, configFilePath); err != nil { + return nil, server.Config{}, err + } + + ctx := context.Background() + + // default is error level + lvl, err := utils.ParseLogLevel(config, logrus.ErrorLevel) + if err != nil { + return nil, server.Config{}, err + } + logrus.SetLevel(lvl) + + // parse bugsnag config + bugsnagConf, err := utils.ParseBugsnag(config) + if err != nil { + return ctx, server.Config{}, err + } + utils.SetUpBugsnag(bugsnagConf) + + trust, keyAlgo, err := getTrustService(config, client.NewNotarySigner, hRegister) + if err != nil { + return nil, server.Config{}, err + } + ctx = context.WithValue(ctx, "keyAlgorithm", keyAlgo) + + store, err := getStore(config, []string{utils.MySQLBackend, utils.MemoryBackend}, hRegister) + if err != nil { + return nil, server.Config{}, err + } + ctx = context.WithValue(ctx, "metaStore", store) + + currentCache, consistentCache, err := getCacheConfig(config) + if err != nil { + return nil, server.Config{}, err + } + + httpAddr, tlsConfig, err := getAddrAndTLSConfig(config) + if err != nil { + return nil, server.Config{}, err + } + + return ctx, server.Config{ + Addr: httpAddr, + TLSConfig: tlsConfig, + Trust: trust, + AuthMethod: config.GetString("auth.type"), + AuthOpts: config.Get("auth.options"), + CurrentCacheControlConfig: currentCache, + ConsistentCacheControlConfig: consistentCache, + }, nil +} diff --git a/cmd/notary-server/main.go b/cmd/notary-server/main.go index cda319d5d0..ae3da854d5 100644 --- a/cmd/notary-server/main.go +++ b/cmd/notary-server/main.go @@ -10,13 +10,8 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/distribution/health" - "github.com/docker/notary/signer/client" - "golang.org/x/net/context" - "github.com/docker/notary/server" - "github.com/docker/notary/utils" "github.com/docker/notary/version" - "github.com/spf13/viper" ) // DebugAddress is the debug server address to listen on @@ -44,64 +39,6 @@ func init() { } } -func parseServerConfig(configFilePath string, hRegister healthRegister) (context.Context, server.Config, error) { - config := viper.New() - utils.SetupViper(config, envPrefix) - - // parse viper config - if err := utils.ParseViper(config, configFilePath); err != nil { - return nil, server.Config{}, err - } - - ctx := context.Background() - - // default is error level - lvl, err := utils.ParseLogLevel(config, logrus.ErrorLevel) - if err != nil { - return nil, server.Config{}, err - } - logrus.SetLevel(lvl) - - // parse bugsnag config - bugsnagConf, err := utils.ParseBugsnag(config) - if err != nil { - return ctx, server.Config{}, err - } - utils.SetUpBugsnag(bugsnagConf) - - trust, keyAlgo, err := getTrustService(config, client.NewNotarySigner, hRegister) - if err != nil { - return nil, server.Config{}, err - } - ctx = context.WithValue(ctx, "keyAlgorithm", keyAlgo) - - store, err := getStore(config, []string{utils.MySQLBackend, utils.MemoryBackend}, hRegister) - if err != nil { - return nil, server.Config{}, err - } - ctx = context.WithValue(ctx, "metaStore", store) - - currentCache, consistentCache, err := getCacheConfig(config) - if err != nil { - return nil, server.Config{}, err - } - - httpAddr, tlsConfig, err := getAddrAndTLSConfig(config) - if err != nil { - return nil, server.Config{}, err - } - - return ctx, server.Config{ - Addr: httpAddr, - TLSConfig: tlsConfig, - Trust: trust, - AuthMethod: config.GetString("auth.type"), - AuthOpts: config.Get("auth.options"), - CurrentCacheControlConfig: currentCache, - ConsistentCacheControlConfig: consistentCache, - }, nil -} - func main() { flag.Usage = usage flag.Parse()