From 5b6cc990c0e20f8f624f17f0b07d2880f49eceaa Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Mon, 28 Jun 2021 13:24:46 +0300 Subject: [PATCH] [#71] Fix yaml config support Signed-off-by: Denis Kirillov --- README.md | 38 +++++++++++++++++++++++++++++++++++++- settings.go | 22 +++++++++++----------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 17a5735..87d4788 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ $ HTTP_GW_PEERS_0_ADDRESS=grpcs://192.168.130.72:8080 neofs-http-gw In general, everything available as CLI parameter can also be specified via environment variables, so they're not specifically mentioned in most cases -(see `--help` also). +(see `--help` also). If you prefer a config file you can use it in yaml format. ### Nodes and weights @@ -171,6 +171,42 @@ HTTP_GW_LOGGER_SAMPLING_THEREAFTER=int - Logger sampling thereafter HTTP_GW_LOGGER_TRACE_LEVEL=string - Logger show trace on level ``` +### Yaml file +Configuration file is optional and can be used instead of environment variables/other parameters. +It can be specified with `--config` parameter: +``` +$ neofs-http-gw --config your-config.yaml +``` + +Configuration file example: +``` +listen_address: 0.0.0.0:8082 + +wallet: + passphrase: 123456 + +logger: + level: debug + +peers: + 0: + address: grpc://s01.neofs.devenv:8080 + weight: 1 +``` + +To know nesting level of variable you need to cut off the prefix `HTTP_GW` from variable and split the rest parts by `_`. +For example variable `HTTP_GW_PEERS_0_WEIGHT=1` will be transformed to: +``` +peers: + 0: + weight: 1 +``` + +If parameter doesn't support environment variable (e.g. `--listen_address 0.0.0.0:8082`) form it is used as is: +``` +listen_address: 0.0.0.0:8082 +``` + ## HTTP API provided This gateway intentionally provides limited feature set and doesn't try to diff --git a/settings.go b/settings.go index 983ac84..ceffec6 100644 --- a/settings.go +++ b/settings.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io" "os" "sort" "strconv" @@ -14,11 +13,7 @@ import ( "github.com/valyala/fasthttp" ) -type empty int - const ( - devNull = empty(0) - defaultRebalanceTimer = 15 * time.Second defaultRequestTimeout = 15 * time.Second defaultConnectTimeout = 30 * time.Second @@ -70,6 +65,7 @@ const ( cmdMetrics = "metrics" cmdWallet = "wallet" cmdAddress = "address" + cmdConfig = "config" ) var ignore = map[string]struct{}{ @@ -80,12 +76,11 @@ var ignore = map[string]struct{}{ cmdVersion: {}, } -func (empty) Read([]byte) (int, error) { return 0, io.EOF } - func settings() *viper.Viper { v := viper.New() v.AutomaticEnv() v.SetEnvPrefix(Prefix) + v.AllowEmptyEnv(true) v.SetConfigType("yaml") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) @@ -102,6 +97,7 @@ func settings() *viper.Viper { flags.StringP(cmdWallet, "w", "", `path to the wallet`) flags.String(cmdAddress, "", `address of wallet account`) + config := flags.String(cmdConfig, "", "config path") flags.Bool(cmdVerbose, false, "debug gRPC connections") flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout") flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout") @@ -142,10 +138,6 @@ func settings() *viper.Viper { panic(err) } - if err := v.ReadConfig(devNull); err != nil { - panic(err) - } - if err := flags.Parse(os.Args); err != nil { panic(err) } @@ -183,6 +175,14 @@ func settings() *viper.Viper { os.Exit(0) } + if v.IsSet(cmdConfig) { + if cfgFile, err := os.Open(*config); err != nil { + panic(err) + } else if err := v.ReadConfig(cfgFile); err != nil { + panic(err) + } + } + if peers != nil && len(*peers) > 0 { for i := range *peers { v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i])