From 31186382e8e9e8fd97d4f8b4ddd04f2daa5a1d5c Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Fri, 27 Nov 2020 18:18:53 +0300 Subject: [PATCH] Change ENV prefix TODO should be replaced with HTTP_GW before release Signed-off-by: Evgeniy Kulikov --- Dockerfile | 7 ++++- main.go | 18 ++++++------ misc.go | 3 ++ settings.go | 84 ++++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 88 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index a6f62ba..35365b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,12 @@ COPY . /src ARG VERSION=dev ENV LDFLAGS "-w -s -X main.Version=${VERSION}" RUN set -x \ - && go build -v -mod=vendor -trimpath -ldflags "${LDFLAGS} -X main.Build=$(date -u +%s%N)" -o /go/bin/neofs-gw ./ \ + && go build \ + -v \ + -mod=vendor \ + -trimpath \ + -ldflags "${LDFLAGS} -X main.Build=$(date -u +%s%N) -X main.Prefix=HTTP_GW" \ + -o /go/bin/neofs-gw ./ \ && upx -3 /go/bin/neofs-gw # Executable image diff --git a/main.go b/main.go index 36c12f1..823e982 100644 --- a/main.go +++ b/main.go @@ -9,23 +9,23 @@ import ( func newLogger(v *viper.Viper) *zap.Logger { options := []logger.Option{ - logger.WithLevel(v.GetString("logger.level")), - logger.WithTraceLevel(v.GetString("logger.trace_level")), + logger.WithLevel(v.GetString(cfgLoggerLevel)), + logger.WithTraceLevel(v.GetString(cfgLoggerTraceLevel)), - logger.WithFormat(v.GetString("logger.format")), + logger.WithFormat(v.GetString(cfgLoggerFormat)), - logger.WithSamplingInitial(v.GetInt("logger.sampling.initial")), - logger.WithSamplingThereafter(v.GetInt("logger.sampling.thereafter")), + logger.WithSamplingInitial(v.GetInt(cfgLoggerSamplingInitial)), + logger.WithSamplingThereafter(v.GetInt(cfgLoggerSamplingThereafter)), - logger.WithAppName(v.GetString("app_name")), - logger.WithAppVersion(v.GetString("app_version")), + logger.WithAppName(v.GetString(cfgApplicationName)), + logger.WithAppVersion(v.GetString(cfgApplicationVersion)), } - if v.GetBool("logger.no_caller") { + if v.GetBool(cfgLoggerNoCaller) { options = append(options, logger.WithoutCaller()) } - if v.GetBool("logger.no_disclaimer") { + if v.GetBool(cfgLoggerNoDisclaimer) { options = append(options, logger.WithoutDisclaimer()) } diff --git a/misc.go b/misc.go index a2bc3df..ea82ef6 100644 --- a/misc.go +++ b/misc.go @@ -3,4 +3,7 @@ package main var ( Build = "now" Version = "dev" + + // TODO should be replaced with HTTP_GW + Prefix = "GW" ) diff --git a/settings.go b/settings.go index 3a413e4..24bf29e 100644 --- a/settings.go +++ b/settings.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "sort" "strconv" "strings" "time" @@ -28,14 +29,46 @@ const ( defaultKeepaliveTime = 10 * time.Second defaultKeepaliveTimeout = 10 * time.Second + + // Logger: + cfgLoggerLevel = "logger.level" + cfgLoggerFormat = "logger.format" + cfgLoggerTraceLevel = "logger.trace_level" + cfgLoggerNoCaller = "logger.no_caller" + cfgLoggerNoDisclaimer = "logger.no_disclaimer" + cfgLoggerSamplingInitial = "logger.sampling.initial" + cfgLoggerSamplingThereafter = "logger.sampling.thereafter" + + // Peers + cfgPeers = "peers" + + // Application + cfgApplicationName = "app.name" + cfgApplicationVersion = "app.version" + cfgApplicationBuildTime = "app.build_time" + + // command line args + cmdHelp = "help" + cmdVersion = "version" ) +var ignore = map[string]struct{}{ + cfgApplicationName: {}, + cfgApplicationVersion: {}, + cfgApplicationBuildTime: {}, + + cfgPeers: {}, + + cmdHelp: {}, + cmdVersion: {}, +} + func (empty) Read([]byte) (int, error) { return 0, io.EOF } func settings() *viper.Viper { v := viper.New() v.AutomaticEnv() - v.SetEnvPrefix("GW") + v.SetEnvPrefix(Prefix) v.SetConfigType("yaml") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) @@ -46,8 +79,8 @@ func settings() *viper.Viper { flags.Bool("pprof", false, "enable pprof") flags.Bool("metrics", false, "enable prometheus") - help := flags.BoolP("help", "h", false, "show help") - version := flags.BoolP("version", "v", false, "show version") + help := flags.BoolP(cmdHelp, "h", false, "show help") + version := flags.BoolP(cmdVersion, "v", false, "show version") flags.String("key", generated, `"`+generated+`" to generate key, path to private key file, hex string or wif`) @@ -62,19 +95,19 @@ func settings() *viper.Viper { peers := flags.StringArrayP("peers", "p", nil, "NeoFS nodes") // set prefers: - v.Set("app.name", "neofs-gw") - v.Set("app.version", Version) + v.Set(cfgApplicationName, "neofs-http-gw") + v.Set(cfgApplicationVersion, Version) // set defaults: // logger: - v.SetDefault("logger.level", "debug") - v.SetDefault("logger.format", "console") - v.SetDefault("logger.trace_level", "fatal") - v.SetDefault("logger.no_caller", false) - v.SetDefault("logger.no_disclaimer", true) - v.SetDefault("logger.sampling.initial", 1000) - v.SetDefault("logger.sampling.thereafter", 1000) + v.SetDefault(cfgLoggerLevel, "debug") + v.SetDefault(cfgLoggerFormat, "console") + v.SetDefault(cfgLoggerTraceLevel, "panic") + v.SetDefault(cfgLoggerNoCaller, false) + v.SetDefault(cfgLoggerNoDisclaimer, true) + v.SetDefault(cfgLoggerSamplingInitial, 1000) + v.SetDefault(cfgLoggerSamplingThereafter, 1000) // keepalive: // If set below 10s, a minimum value of 10s will be used instead. @@ -105,6 +138,29 @@ func settings() *viper.Viper { case help != nil && *help: fmt.Printf("NeoFS HTTP Gateway %s (%s)\n", Version, Build) flags.PrintDefaults() + + fmt.Println() + fmt.Println("Default environments:") + fmt.Println() + keys := v.AllKeys() + sort.Strings(keys) + + for i := range keys { + if _, ok := ignore[keys[i]]; ok { + continue + } + + k := strings.Replace(keys[i], ".", "_", -1) + fmt.Printf("%s_%s = %v\n", Prefix, strings.ToUpper(k), v.Get(keys[i])) + } + + fmt.Println() + fmt.Println("Peers preset:") + fmt.Println() + + fmt.Printf("%s_%s_[N]_ADDRESS = string\n", Prefix, strings.ToUpper(cfgPeers)) + fmt.Printf("%s_%s_[N]_WEIGHT = 0..1 (float)\n", Prefix, strings.ToUpper(cfgPeers)) + os.Exit(0) case version != nil && *version: fmt.Printf("NeoFS HTTP Gateway %s (%s)\n", Version, Build) @@ -115,8 +171,8 @@ func settings() *viper.Viper { if peers != nil && len(*peers) > 0 { for i := range *peers { - v.SetDefault("peers."+strconv.Itoa(i)+".address", (*peers)[i]) - v.SetDefault("peers."+strconv.Itoa(i)+".weight", 1) + v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i]) + v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".weight", 1) } }