[#101] Fix yaml config support

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-06-28 13:44:22 +03:00
parent ce65a47d1b
commit 69b2004a2b
2 changed files with 47 additions and 12 deletions

View file

@ -57,7 +57,7 @@ $ S3_GW_PEERS_0_ADDRESS=grpcs://192.168.130.72:8080 \
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
@ -107,6 +107,42 @@ Pprof and Prometheus are integrated into the gateway, but not enabled by
default. To enable them use `--pprof` and `--metrics` flags or default. To enable them use `--pprof` and `--metrics` flags or
`HTTP_GW_PPROF`/`HTTP_GW_METRICS` environment variables. `HTTP_GW_PPROF`/`HTTP_GW_METRICS` environment variables.
### Yaml file
Configuration file is optional and can be used instead of environment variables/other parameters.
It can be specified with `--config` parameter:
```
$ neofs-s3-gw --config your-config.yaml
```
Configuration file example:
```
listen_address: 0.0.0.0:8084
wallet:
passphrase: 123456
logger:
level: debug
peers:
0:
address: s01.neofs.devenv:8080
weight: 1
```
To know nesting level of variable you need to cut off the prefix `S3_GW` from variable and split the rest parts by `_`.
For example variable `S3_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:8084`) form it is used as is:
```
listen_address: 0.0.0.0:8084
```
## NeoFS AuthMate ## NeoFS AuthMate
Authmate is a tool to create gateway AWS credentials. AWS users Authmate is a tool to create gateway AWS credentials. AWS users

View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"sort" "sort"
"strconv" "strconv"
@ -17,8 +16,6 @@ import (
) )
const ( const (
devNull = empty(0)
minimumTTLInMinutes = 5 minimumTTLInMinutes = 5
defaultTTL = minimumTTLInMinutes * time.Minute defaultTTL = minimumTTLInMinutes * time.Minute
@ -81,6 +78,7 @@ const ( // Settings.
// Command line args. // Command line args.
cmdHelp = "help" cmdHelp = "help"
cmdVersion = "version" cmdVersion = "version"
cmdConfig = "config"
// applicationName is gateway name. // applicationName is gateway name.
applicationName = "neofs-s3-gw" applicationName = "neofs-s3-gw"
@ -89,8 +87,6 @@ const ( // Settings.
envPrefix = "S3_GW" envPrefix = "S3_GW"
) )
type empty int
var ignore = map[string]struct{}{ var ignore = map[string]struct{}{
cfgApplicationName: {}, cfgApplicationName: {},
cfgApplicationVersion: {}, cfgApplicationVersion: {},
@ -102,8 +98,6 @@ var ignore = map[string]struct{}{
cmdVersion: {}, cmdVersion: {},
} }
func (empty) Read([]byte) (int, error) { return 0, io.EOF }
func fetchPeers(l *zap.Logger, v *viper.Viper) *pool.Builder { func fetchPeers(l *zap.Logger, v *viper.Viper) *pool.Builder {
pb := new(pool.Builder) pb := new(pool.Builder)
@ -166,6 +160,7 @@ func newSettings() *viper.Viper {
flags.StringP(cfgWallet, "w", "", `path to the wallet`) flags.StringP(cfgWallet, "w", "", `path to the wallet`)
flags.String(cfgAddress, "", `address of wallet account`) flags.String(cfgAddress, "", `address of wallet account`)
config := flags.String(cmdConfig, "", "config path")
flags.Bool(cfgGRPCVerbose, false, "set debug mode of gRPC connections") flags.Bool(cfgGRPCVerbose, false, "set debug mode of gRPC connections")
flags.Duration(cfgRequestTimeout, defaultRequestTimeout, "set gRPC request timeout") flags.Duration(cfgRequestTimeout, defaultRequestTimeout, "set gRPC request timeout")
@ -204,10 +199,6 @@ func newSettings() *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)
} }
@ -262,5 +253,13 @@ func newSettings() *viper.Viper {
fmt.Printf("connection ttl should not be less than %s", defaultTTL) fmt.Printf("connection ttl should not be less than %s", defaultTTL)
} }
if v.IsSet(cmdConfig) {
if cfgFile, err := os.Open(*config); err != nil {
panic(err)
} else if err := v.ReadConfig(cfgFile); err != nil {
panic(err)
}
}
return v return v
} }