2019-12-13 16:02:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-07-18 08:52:30 +00:00
|
|
|
"runtime"
|
2020-11-27 15:18:53 +00:00
|
|
|
"sort"
|
2019-12-21 10:26:14 +00:00
|
|
|
"strconv"
|
2019-12-13 16:02:48 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/resolver"
|
2019-12-13 16:02:48 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
2021-02-12 12:24:52 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-19 14:06:32 +00:00
|
|
|
defaultRebalanceTimer = 60 * time.Second
|
2020-02-19 10:47:21 +00:00
|
|
|
defaultRequestTimeout = 15 * time.Second
|
2022-07-19 14:06:32 +00:00
|
|
|
defaultConnectTimeout = 10 * time.Second
|
2020-02-19 10:47:21 +00:00
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
defaultShutdownTimeout = 15 * time.Second
|
|
|
|
|
2022-07-29 06:34:26 +00:00
|
|
|
defaultPoolErrorThreshold uint32 = 100
|
|
|
|
|
2021-04-15 14:12:24 +00:00
|
|
|
cfgListenAddress = "listen_address"
|
|
|
|
cfgTLSCertificate = "tls_certificate"
|
|
|
|
cfgTLSKey = "tls_key"
|
2020-12-02 09:09:46 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Web.
|
2021-02-12 12:24:52 +00:00
|
|
|
cfgWebReadBufferSize = "web.read_buffer_size"
|
|
|
|
cfgWebWriteBufferSize = "web.write_buffer_size"
|
|
|
|
cfgWebReadTimeout = "web.read_timeout"
|
|
|
|
cfgWebWriteTimeout = "web.write_timeout"
|
|
|
|
cfgWebStreamRequestBody = "web.stream_request_body"
|
|
|
|
cfgWebMaxRequestBodySize = "web.max_request_body_size"
|
2020-12-02 09:09:46 +00:00
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
// Metrics / Profiler.
|
|
|
|
cfgPrometheusEnabled = "prometheus.enabled"
|
|
|
|
cfgPrometheusAddress = "prometheus.address"
|
|
|
|
cfgPprofEnabled = "pprof.enabled"
|
|
|
|
cfgPprofAddress = "pprof.address"
|
|
|
|
|
2022-07-29 06:34:26 +00:00
|
|
|
// Pool config.
|
|
|
|
cfgConTimeout = "connect_timeout"
|
|
|
|
cfgReqTimeout = "request_timeout"
|
|
|
|
cfgRebalance = "rebalance_timer"
|
|
|
|
cfgPoolErrorThreshold = "pool_error_threshold"
|
2020-12-02 09:09:46 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Logger.
|
2022-03-25 13:06:33 +00:00
|
|
|
cfgLoggerLevel = "logger.level"
|
2020-11-27 15:18:53 +00:00
|
|
|
|
2021-06-22 11:53:59 +00:00
|
|
|
// Wallet.
|
|
|
|
cfgWalletPassphrase = "wallet.passphrase"
|
2022-04-08 09:01:57 +00:00
|
|
|
cfgWalletPath = "wallet.path"
|
|
|
|
cfgWalletAddress = "wallet.address"
|
2021-06-22 11:53:59 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Uploader Header.
|
2021-01-26 09:40:01 +00:00
|
|
|
cfgUploaderHeaderEnableDefaultTimestamp = "upload_header.use_default_timestamp"
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Peers.
|
2020-11-27 15:18:53 +00:00
|
|
|
cfgPeers = "peers"
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
// NeoGo.
|
|
|
|
cfgRPCEndpoint = "rpc_endpoint"
|
|
|
|
|
|
|
|
// Resolving.
|
|
|
|
cfgResolveOrder = "resolve_order"
|
|
|
|
|
2021-10-29 13:11:34 +00:00
|
|
|
// Zip compression.
|
|
|
|
cfgZipCompression = "zip.compression"
|
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Command line args.
|
2021-06-22 11:53:59 +00:00
|
|
|
cmdHelp = "help"
|
|
|
|
cmdVersion = "version"
|
|
|
|
cmdPprof = "pprof"
|
|
|
|
cmdMetrics = "metrics"
|
|
|
|
cmdWallet = "wallet"
|
|
|
|
cmdAddress = "address"
|
2021-06-28 10:24:46 +00:00
|
|
|
cmdConfig = "config"
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
2020-11-27 15:18:53 +00:00
|
|
|
var ignore = map[string]struct{}{
|
2022-03-25 13:06:33 +00:00
|
|
|
cfgPeers: {},
|
|
|
|
cmdHelp: {},
|
|
|
|
cmdVersion: {},
|
2020-11-27 15:18:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
func settings() *viper.Viper {
|
|
|
|
v := viper.New()
|
|
|
|
v.AutomaticEnv()
|
2020-11-27 15:18:53 +00:00
|
|
|
v.SetEnvPrefix(Prefix)
|
2021-06-28 10:24:46 +00:00
|
|
|
v.AllowEmptyEnv(true)
|
2019-12-13 16:02:48 +00:00
|
|
|
v.SetConfigType("yaml")
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
|
|
|
|
// flags setup:
|
2019-12-21 10:26:14 +00:00
|
|
|
flags := pflag.NewFlagSet("commandline", pflag.ExitOnError)
|
2020-12-02 08:52:07 +00:00
|
|
|
flags.SetOutput(os.Stdout)
|
2019-12-13 16:02:48 +00:00
|
|
|
flags.SortFlags = false
|
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Bool(cmdPprof, false, "enable pprof")
|
|
|
|
flags.Bool(cmdMetrics, false, "enable prometheus")
|
2019-12-21 10:26:14 +00:00
|
|
|
|
2020-11-27 15:18:53 +00:00
|
|
|
help := flags.BoolP(cmdHelp, "h", false, "show help")
|
|
|
|
version := flags.BoolP(cmdVersion, "v", false, "show version")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2021-06-22 11:53:59 +00:00
|
|
|
flags.StringP(cmdWallet, "w", "", `path to the wallet`)
|
|
|
|
flags.String(cmdAddress, "", `address of wallet account`)
|
2021-06-28 10:24:46 +00:00
|
|
|
config := flags.String(cmdConfig, "", "config path")
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout")
|
|
|
|
flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout")
|
|
|
|
flags.Duration(cfgRebalance, defaultRebalanceTimer, "gRPC connection rebalance timer")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
flags.String(cfgListenAddress, "0.0.0.0:8082", "address to listen")
|
2021-04-15 14:12:24 +00:00
|
|
|
flags.String(cfgTLSCertificate, "", "TLS certificate path")
|
|
|
|
flags.String(cfgTLSKey, "", "TLS key path")
|
2020-12-02 09:09:46 +00:00
|
|
|
peers := flags.StringArrayP(cfgPeers, "p", nil, "NeoFS nodes")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
resolveMethods := flags.StringSlice(cfgResolveOrder, []string{resolver.NNSResolver, resolver.DNSResolver}, "set container name resolve order")
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
// set defaults:
|
|
|
|
|
|
|
|
// logger:
|
2020-11-27 15:18:53 +00:00
|
|
|
v.SetDefault(cfgLoggerLevel, "debug")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2022-07-29 06:34:26 +00:00
|
|
|
// pool:
|
|
|
|
v.SetDefault(cfgPoolErrorThreshold, defaultPoolErrorThreshold)
|
|
|
|
|
2020-04-15 08:10:42 +00:00
|
|
|
// web-server:
|
2020-12-02 09:09:46 +00:00
|
|
|
v.SetDefault(cfgWebReadBufferSize, 4096)
|
|
|
|
v.SetDefault(cfgWebWriteBufferSize, 4096)
|
2022-04-21 15:53:17 +00:00
|
|
|
v.SetDefault(cfgWebReadTimeout, time.Minute*10)
|
|
|
|
v.SetDefault(cfgWebWriteTimeout, time.Minute*5)
|
2021-02-12 12:24:52 +00:00
|
|
|
v.SetDefault(cfgWebStreamRequestBody, true)
|
|
|
|
v.SetDefault(cfgWebMaxRequestBodySize, fasthttp.DefaultMaxRequestBodySize)
|
2020-04-15 08:10:42 +00:00
|
|
|
|
2021-01-26 09:40:01 +00:00
|
|
|
// upload header
|
|
|
|
v.SetDefault(cfgUploaderHeaderEnableDefaultTimestamp, false)
|
|
|
|
|
2021-10-29 13:11:34 +00:00
|
|
|
// zip:
|
|
|
|
v.SetDefault(cfgZipCompression, false)
|
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
// metrics
|
|
|
|
v.SetDefault(cfgPprofAddress, "localhost:8083")
|
|
|
|
v.SetDefault(cfgPrometheusAddress, "localhost:8084")
|
|
|
|
|
|
|
|
// Binding flags
|
|
|
|
if err := v.BindPFlag(cfgPprofEnabled, flags.Lookup(cmdPprof)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgPrometheusEnabled, flags.Lookup(cmdMetrics)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if err := v.BindPFlags(flags); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := flags.Parse(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
if resolveMethods != nil {
|
|
|
|
v.SetDefault(cfgResolveOrder, *resolveMethods)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
switch {
|
|
|
|
case help != nil && *help:
|
2021-05-25 10:19:40 +00:00
|
|
|
fmt.Printf("NeoFS HTTP Gateway %s\n", Version)
|
2019-12-13 16:02:48 +00:00
|
|
|
flags.PrintDefaults()
|
2020-11-27 15:18:53 +00:00
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Default environments:")
|
|
|
|
fmt.Println()
|
|
|
|
keys := v.AllKeys()
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for i := range keys {
|
|
|
|
if _, ok := ignore[keys[i]]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
k := strings.Replace(keys[i], ".", "_", -1)
|
|
|
|
fmt.Printf("%s_%s = %v\n", Prefix, strings.ToUpper(k), v.Get(keys[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Peers preset:")
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Printf("%s_%s_[N]_ADDRESS = string\n", Prefix, strings.ToUpper(cfgPeers))
|
2021-04-14 20:53:47 +00:00
|
|
|
fmt.Printf("%s_%s_[N]_WEIGHT = float\n", Prefix, strings.ToUpper(cfgPeers))
|
2020-11-27 15:18:53 +00:00
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
os.Exit(0)
|
|
|
|
case version != nil && *version:
|
2022-07-18 08:52:30 +00:00
|
|
|
fmt.Printf("NeoFS HTTP Gateway\nVersion: %s\nGoVersion: %s\n", Version, runtime.Version())
|
2019-12-13 16:02:48 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:24:46 +00:00
|
|
|
if v.IsSet(cmdConfig) {
|
|
|
|
if cfgFile, err := os.Open(*config); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if err := v.ReadConfig(cfgFile); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
if peers != nil && len(*peers) > 0 {
|
|
|
|
for i := range *peers {
|
2020-11-27 15:18:53 +00:00
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i])
|
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".weight", 1)
|
2021-12-07 11:57:16 +00:00
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".priority", 1)
|
2019-12-21 10:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
return v
|
|
|
|
}
|