Merge pull request #74 from KirillovDenis/feature/71-yaml_config

[#71] Fix yaml config support
This commit is contained in:
Angira Kekteeva 2021-06-28 17:23:30 +03:00 committed by GitHub
commit 6cc9faad00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 12 deletions

View file

@ -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 In general, everything available as CLI parameter can also be specified via
environment variables, so they're not specifically mentioned in most cases 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 ### 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 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 ## HTTP API provided
This gateway intentionally provides limited feature set and doesn't try to This gateway intentionally provides limited feature set and doesn't try to

View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"sort" "sort"
"strconv" "strconv"
@ -14,11 +13,7 @@ import (
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
type empty int
const ( const (
devNull = empty(0)
defaultRebalanceTimer = 15 * time.Second defaultRebalanceTimer = 15 * time.Second
defaultRequestTimeout = 15 * time.Second defaultRequestTimeout = 15 * time.Second
defaultConnectTimeout = 30 * time.Second defaultConnectTimeout = 30 * time.Second
@ -70,6 +65,7 @@ const (
cmdMetrics = "metrics" cmdMetrics = "metrics"
cmdWallet = "wallet" cmdWallet = "wallet"
cmdAddress = "address" cmdAddress = "address"
cmdConfig = "config"
) )
var ignore = map[string]struct{}{ var ignore = map[string]struct{}{
@ -80,12 +76,11 @@ var ignore = map[string]struct{}{
cmdVersion: {}, cmdVersion: {},
} }
func (empty) Read([]byte) (int, error) { return 0, io.EOF }
func settings() *viper.Viper { func settings() *viper.Viper {
v := viper.New() v := viper.New()
v.AutomaticEnv() v.AutomaticEnv()
v.SetEnvPrefix(Prefix) v.SetEnvPrefix(Prefix)
v.AllowEmptyEnv(true)
v.SetConfigType("yaml") v.SetConfigType("yaml")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
@ -102,6 +97,7 @@ func settings() *viper.Viper {
flags.StringP(cmdWallet, "w", "", `path to the wallet`) flags.StringP(cmdWallet, "w", "", `path to the wallet`)
flags.String(cmdAddress, "", `address of wallet account`) flags.String(cmdAddress, "", `address of wallet account`)
config := flags.String(cmdConfig, "", "config path")
flags.Bool(cmdVerbose, false, "debug gRPC connections") flags.Bool(cmdVerbose, false, "debug gRPC connections")
flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout") flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout")
flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout") flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout")
@ -142,10 +138,6 @@ func settings() *viper.Viper {
panic(err) panic(err)
} }
if err := v.ReadConfig(devNull); err != nil {
panic(err)
}
if err := flags.Parse(os.Args); err != nil { if err := flags.Parse(os.Args); err != nil {
panic(err) panic(err)
} }
@ -183,6 +175,14 @@ func settings() *viper.Viper {
os.Exit(0) 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 { if peers != nil && len(*peers) > 0 {
for i := range *peers { for i := range *peers {
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i]) v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i])