Use constant settings keys

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-12-02 12:09:46 +03:00
parent 01f13ef210
commit 8bd210d67e
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2
2 changed files with 59 additions and 67 deletions

57
app.go
View file

@ -3,8 +3,6 @@ package main
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"strconv" "strconv"
"github.com/fasthttp/router" "github.com/fasthttp/router"
@ -12,7 +10,6 @@ import (
"github.com/nspcc-dev/cdn-neofs-sdk/creds/neofs" "github.com/nspcc-dev/cdn-neofs-sdk/creds/neofs"
"github.com/nspcc-dev/cdn-neofs-sdk/logger" "github.com/nspcc-dev/cdn-neofs-sdk/logger"
"github.com/nspcc-dev/cdn-neofs-sdk/pool" "github.com/nspcc-dev/cdn-neofs-sdk/pool"
crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/zap" "go.uber.org/zap"
@ -79,20 +76,20 @@ func newApp(ctx context.Context, opt ...Option) App {
a.wlog = logger.GRPC(a.log) a.wlog = logger.GRPC(a.log)
if a.cfg.GetBool("verbose") { if a.cfg.GetBool(cmdVerbose) {
grpclog.SetLoggerV2(a.wlog) grpclog.SetLoggerV2(a.wlog)
} }
conTimeout := a.cfg.GetDuration("connect_timeout") conTimeout := a.cfg.GetDuration(cfgConTimeout)
reqTimeout := a.cfg.GetDuration("request_timeout") reqTimeout := a.cfg.GetDuration(cfgReqTimeout)
tckTimeout := a.cfg.GetDuration("rebalance_timer") tckTimeout := a.cfg.GetDuration(cfgRebalance)
// -- setup FastHTTP server: -- // -- setup FastHTTP server: --
a.web.Name = "neofs-http-gate" a.web.Name = "neofs-http-gate"
a.web.ReadBufferSize = a.cfg.GetInt("web.read_buffer_size") a.web.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
a.web.WriteBufferSize = a.cfg.GetInt("web.write_buffer_size") a.web.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
a.web.ReadTimeout = a.cfg.GetDuration("web.read_timeout") a.web.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
a.web.WriteTimeout = a.cfg.GetDuration("web.write_timeout") a.web.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
a.web.GetOnly = true a.web.GetOnly = true
a.web.DisableHeaderNamesNormalizing = true a.web.DisableHeaderNamesNormalizing = true
a.web.NoDefaultServerHeader = true a.web.NoDefaultServerHeader = true
@ -101,8 +98,8 @@ func newApp(ctx context.Context, opt ...Option) App {
connections := make(map[string]float64) connections := make(map[string]float64)
for i := 0; ; i++ { for i := 0; ; i++ {
address := a.cfg.GetString("peers." + strconv.Itoa(i) + ".address") address := a.cfg.GetString(cfgPeers + "." + strconv.Itoa(i) + ".address")
weight := a.cfg.GetFloat64("peers." + strconv.Itoa(i) + ".weight") weight := a.cfg.GetFloat64(cfgPeers + "." + strconv.Itoa(i) + ".weight")
if address == "" { if address == "" {
break break
} }
@ -113,7 +110,7 @@ func newApp(ctx context.Context, opt ...Option) App {
zap.Float64("weight", weight)) zap.Float64("weight", weight))
} }
cred, err := prepareCredentials(a.cfg.GetString("key"), a.log) cred, err := neofs.New(a.cfg.GetString(cmdNeoFSKey))
if err != nil { if err != nil {
a.log.Fatal("could not prepare credentials", zap.Error(err)) a.log.Fatal("could not prepare credentials", zap.Error(err))
} }
@ -130,9 +127,9 @@ func newApp(ctx context.Context, opt ...Option) App {
grpc.WithBlock(), grpc.WithBlock(),
grpc.WithInsecure(), grpc.WithInsecure(),
grpc.WithKeepaliveParams(keepalive.ClientParameters{ grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: a.cfg.GetDuration("keepalive.time"), Time: a.cfg.GetDuration(cfgKeepaliveTime),
Timeout: a.cfg.GetDuration("keepalive.timeout"), Timeout: a.cfg.GetDuration(cfgKeepaliveTimeout),
PermitWithoutStream: a.cfg.GetBool("keepalive.permit_without_stream"), PermitWithoutStream: a.cfg.GetBool(cfgKeepalivePermitWithoutStream),
}))) })))
if err != nil { if err != nil {
@ -151,26 +148,6 @@ func newApp(ctx context.Context, opt ...Option) App {
return a return a
} }
func prepareCredentials(key string, log *zap.Logger) (neofs.Credentials, error) {
if key == generated {
log.Fatal("Don't use generated key, deprecated")
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
key, err = crypto.WIFEncode(sk)
if err != nil {
return nil, err
}
log.Info("generate new key", zap.String("wif", key))
}
return neofs.New(key)
}
func (a *app) Wait() { func (a *app) Wait() {
a.log.Info("application started") a.log.Info("application started")
@ -204,18 +181,18 @@ func (a *app) Serve(ctx context.Context) {
attachHealthy(r, a.pool.Status) attachHealthy(r, a.pool.Status)
// enable metrics // enable metrics
if a.cfg.GetBool("metrics") { if a.cfg.GetBool(cmdMetrics) {
a.log.Info("enabled /metrics/") a.log.Info("enabled /metrics/")
attachMetrics(r, a.wlog) attachMetrics(r, a.wlog)
} }
// enable pprof // enable pprof
if a.cfg.GetBool("pprof") { if a.cfg.GetBool(cmdPprof) {
a.log.Info("enabled /debug/pprof/") a.log.Info("enabled /debug/pprof/")
attachProfiler(r) attachProfiler(r)
} }
bind := a.cfg.GetString("listen_address") bind := a.cfg.GetString(cfgListenAddress)
a.log.Info("run gateway server", a.log.Info("run gateway server",
zap.String("address", bind)) zap.String("address", bind))

View file

@ -19,10 +19,6 @@ const (
devNull = empty(0) devNull = empty(0)
generated = "generated" generated = "generated"
minimumTTLInMinutes = 5
defaultTTL = minimumTTLInMinutes * time.Minute
defaultRebalanceTimer = 15 * time.Second defaultRebalanceTimer = 15 * time.Second
defaultRequestTimeout = 15 * time.Second defaultRequestTimeout = 15 * time.Second
defaultConnectTimeout = 30 * time.Second defaultConnectTimeout = 30 * time.Second
@ -30,6 +26,25 @@ const (
defaultKeepaliveTime = 10 * time.Second defaultKeepaliveTime = 10 * time.Second
defaultKeepaliveTimeout = 10 * time.Second defaultKeepaliveTimeout = 10 * time.Second
cfgListenAddress = "listen_address"
// KeepAlive
cfgKeepaliveTime = "keepalive.time"
cfgKeepaliveTimeout = "keepalive.timeout"
cfgKeepalivePermitWithoutStream = "keepalive.permit_without_stream"
// Web
cfgWebReadBufferSize = "web.read_buffer_size"
cfgWebWriteBufferSize = "web.write_buffer_size"
cfgWebReadTimeout = "web.read_timeout"
cfgWebWriteTimeout = "web.write_timeout"
cfgWebConnectionPerHost = "web.connection_per_host"
// Timeouts
cfgConTimeout = "con_timeout"
cfgReqTimeout = "req_timeout"
cfgRebalance = "rebalance_timer"
// Logger: // Logger:
cfgLoggerLevel = "logger.level" cfgLoggerLevel = "logger.level"
cfgLoggerFormat = "logger.format" cfgLoggerFormat = "logger.format"
@ -50,6 +65,10 @@ const (
// command line args // command line args
cmdHelp = "help" cmdHelp = "help"
cmdVersion = "version" cmdVersion = "version"
cmdVerbose = "verbose"
cmdPprof = "pprof"
cmdMetrics = "metrics"
cmdNeoFSKey = "key"
) )
var ignore = map[string]struct{}{ var ignore = map[string]struct{}{
@ -77,23 +96,21 @@ func settings() *viper.Viper {
flags.SetOutput(os.Stdout) flags.SetOutput(os.Stdout)
flags.SortFlags = false flags.SortFlags = false
flags.Bool("pprof", false, "enable pprof") flags.Bool(cmdPprof, false, "enable pprof")
flags.Bool("metrics", false, "enable prometheus") flags.Bool(cmdMetrics, false, "enable prometheus")
help := flags.BoolP(cmdHelp, "h", false, "show help") help := flags.BoolP(cmdHelp, "h", false, "show help")
version := flags.BoolP(cmdVersion, "v", false, "show version") 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`) flags.String(cmdNeoFSKey, "", `"Path to private key file, hex string or wif`)
flags.Bool("verbose", false, "debug gRPC connections") flags.Bool(cmdVerbose, false, "debug gRPC connections")
flags.Duration("request_timeout", defaultRequestTimeout, "gRPC request timeout") flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout")
flags.Duration("connect_timeout", defaultConnectTimeout, "gRPC connect timeout") flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout")
flags.Duration("rebalance_timer", defaultRebalanceTimer, "gRPC connection rebalance timer") flags.Duration(cfgRebalance, defaultRebalanceTimer, "gRPC connection rebalance timer")
ttl := flags.DurationP("conn_ttl", "t", defaultTTL, "gRPC connection time to live") flags.String(cfgListenAddress, "0.0.0.0:8082", "HTTP Gateway listen address")
peers := flags.StringArrayP(cfgPeers, "p", nil, "NeoFS nodes")
flags.String("listen_address", "0.0.0.0:8082", "HTTP Gateway listen address")
peers := flags.StringArrayP("peers", "p", nil, "NeoFS nodes")
// set prefers: // set prefers:
v.Set(cfgApplicationName, "neofs-http-gw") v.Set(cfgApplicationName, "neofs-http-gw")
@ -112,16 +129,16 @@ func settings() *viper.Viper {
// keepalive: // keepalive:
// If set below 10s, a minimum value of 10s will be used instead. // If set below 10s, a minimum value of 10s will be used instead.
v.SetDefault("keepalive.time", defaultKeepaliveTime) v.SetDefault(cfgKeepaliveTime, defaultKeepaliveTime)
v.SetDefault("keepalive.timeout", defaultKeepaliveTimeout) v.SetDefault(cfgKeepaliveTimeout, defaultKeepaliveTimeout)
v.SetDefault("keepalive.permit_without_stream", true) v.SetDefault(cfgKeepalivePermitWithoutStream, true)
// web-server: // web-server:
v.SetDefault("web.read_buffer_size", 4096) v.SetDefault(cfgWebReadBufferSize, 4096)
v.SetDefault("web.write_buffer_size", 4096) v.SetDefault(cfgWebWriteBufferSize, 4096)
v.SetDefault("web.read_timeout", time.Second*15) v.SetDefault(cfgWebReadTimeout, time.Second*15)
v.SetDefault("web.write_timeout", time.Minute) v.SetDefault(cfgWebWriteTimeout, time.Minute)
v.SetDefault("web.connection_per_host", 10) v.SetDefault(cfgWebConnectionPerHost, 10)
if err := v.BindPFlags(flags); err != nil { if err := v.BindPFlags(flags); err != nil {
panic(err) panic(err)
@ -166,8 +183,6 @@ func settings() *viper.Viper {
case version != nil && *version: case version != nil && *version:
fmt.Printf("NeoFS HTTP Gateway %s (%s)\n", Version, Build) fmt.Printf("NeoFS HTTP Gateway %s (%s)\n", Version, Build)
os.Exit(0) os.Exit(0)
case ttl != nil && ttl.Minutes() < minimumTTLInMinutes:
fmt.Printf("connection ttl should not be less than %s", defaultTTL)
} }
if peers != nil && len(*peers) > 0 { if peers != nil && len(*peers) > 0 {