2019-12-13 16:02:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2019-12-21 10:26:14 +00:00
|
|
|
"strconv"
|
2019-12-13 16:02:48 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type empty int
|
|
|
|
|
|
|
|
const (
|
2019-12-17 15:34:04 +00:00
|
|
|
devNull = empty(0)
|
|
|
|
generated = "generated"
|
2020-02-19 10:47:21 +00:00
|
|
|
|
|
|
|
minimumTTLInMinutes = 5
|
|
|
|
|
|
|
|
defaultTTL = minimumTTLInMinutes * time.Minute
|
|
|
|
|
2020-02-27 10:05:37 +00:00
|
|
|
defaultRebalanceTimer = 15 * time.Second
|
2020-02-19 10:47:21 +00:00
|
|
|
defaultRequestTimeout = 15 * time.Second
|
|
|
|
defaultConnectTimeout = 30 * time.Second
|
|
|
|
|
|
|
|
defaultKeepaliveTime = 10 * time.Second
|
|
|
|
defaultKeepaliveTimeout = 10 * time.Second
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (empty) Read([]byte) (int, error) { return 0, io.EOF }
|
|
|
|
|
|
|
|
func settings() *viper.Viper {
|
|
|
|
v := viper.New()
|
|
|
|
v.AutomaticEnv()
|
|
|
|
v.SetEnvPrefix("GW")
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
|
|
|
|
// flags setup:
|
2019-12-21 10:26:14 +00:00
|
|
|
flags := pflag.NewFlagSet("commandline", pflag.ExitOnError)
|
2019-12-13 16:02:48 +00:00
|
|
|
flags.SortFlags = false
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
flags.Bool("pprof", false, "enable pprof")
|
|
|
|
flags.Bool("metrics", false, "enable prometheus")
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
help := flags.BoolP("help", "h", false, "show help")
|
|
|
|
version := flags.BoolP("version", "v", false, "show version")
|
|
|
|
|
2019-12-17 15:34:04 +00:00
|
|
|
flags.String("key", generated, `"`+generated+`" to generate key, path to private key file, hex string or wif`)
|
2019-12-13 16:02:48 +00:00
|
|
|
|
|
|
|
flags.Bool("verbose", false, "debug gRPC connections")
|
2020-02-19 10:47:21 +00:00
|
|
|
flags.Duration("request_timeout", defaultRequestTimeout, "gRPC request timeout")
|
|
|
|
flags.Duration("connect_timeout", defaultConnectTimeout, "gRPC connect timeout")
|
2020-02-27 10:05:37 +00:00
|
|
|
flags.Duration("rebalance_timer", defaultRebalanceTimer, "gRPC connection rebalance timer")
|
|
|
|
|
2020-02-19 10:47:21 +00:00
|
|
|
ttl := flags.DurationP("conn_ttl", "t", defaultTTL, "gRPC connection time to live")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
|
|
|
flags.String("listen_address", "0.0.0.0:8082", "HTTP Gateway listen address")
|
2019-12-21 10:26:14 +00:00
|
|
|
peers := flags.StringArrayP("peers", "p", nil, "NeoFS nodes")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
|
|
|
// set prefers:
|
|
|
|
v.Set("app.name", "neofs-gw")
|
|
|
|
v.Set("app.version", Version)
|
|
|
|
|
|
|
|
// set defaults:
|
|
|
|
|
|
|
|
// logger:
|
|
|
|
v.SetDefault("logger.level", "debug")
|
|
|
|
v.SetDefault("logger.format", "console")
|
|
|
|
v.SetDefault("logger.trace_level", "fatal")
|
2020-11-09 13:43:23 +00:00
|
|
|
v.SetDefault("logger.no_caller", false)
|
2019-12-13 16:02:48 +00:00
|
|
|
v.SetDefault("logger.no_disclaimer", true)
|
|
|
|
v.SetDefault("logger.sampling.initial", 1000)
|
|
|
|
v.SetDefault("logger.sampling.thereafter", 1000)
|
|
|
|
|
|
|
|
// keepalive:
|
2020-02-19 10:47:21 +00:00
|
|
|
// If set below 10s, a minimum value of 10s will be used instead.
|
|
|
|
v.SetDefault("keepalive.time", defaultKeepaliveTime)
|
|
|
|
v.SetDefault("keepalive.timeout", defaultKeepaliveTimeout)
|
2019-12-13 16:02:48 +00:00
|
|
|
v.SetDefault("keepalive.permit_without_stream", true)
|
|
|
|
|
2020-04-15 08:10:42 +00:00
|
|
|
// web-server:
|
|
|
|
v.SetDefault("web.read_buffer_size", 4096)
|
|
|
|
v.SetDefault("web.write_buffer_size", 4096)
|
|
|
|
v.SetDefault("web.read_timeout", time.Second*15)
|
|
|
|
v.SetDefault("web.write_timeout", time.Minute)
|
|
|
|
v.SetDefault("web.connection_per_host", 10)
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if err := v.BindPFlags(flags); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.ReadConfig(devNull); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := flags.Parse(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case help != nil && *help:
|
|
|
|
fmt.Printf("NeoFS HTTP Gateway %s (%s)\n", Version, Build)
|
|
|
|
flags.PrintDefaults()
|
|
|
|
os.Exit(0)
|
|
|
|
case version != nil && *version:
|
|
|
|
fmt.Printf("NeoFS HTTP Gateway %s (%s)\n", Version, Build)
|
|
|
|
os.Exit(0)
|
2020-02-19 10:47:21 +00:00
|
|
|
case ttl != nil && ttl.Minutes() < minimumTTLInMinutes:
|
|
|
|
fmt.Printf("connection ttl should not be less than %s", defaultTTL)
|
2019-12-13 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
return v
|
|
|
|
}
|