2019-12-13 16:02:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-02-03 09:46:25 +00:00
|
|
|
"path"
|
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-12-14 15:59:24 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-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"
|
2022-09-08 14:57:22 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
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
|
2022-11-15 15:06:53 +00:00
|
|
|
defaultStreamTimeout = 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
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
cfgServer = "server"
|
|
|
|
cfgTLSEnabled = "tls.enabled"
|
|
|
|
cfgTLSCertFile = "tls.cert_file"
|
|
|
|
cfgTLSKeyFile = "tls.key_file"
|
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"
|
2022-11-15 15:06:53 +00:00
|
|
|
cfgStreamTimeout = "stream_timeout"
|
2022-07-29 06:34:26 +00:00
|
|
|
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.
|
2022-11-24 14:49:21 +00:00
|
|
|
cmdHelp = "help"
|
|
|
|
cmdVersion = "version"
|
|
|
|
cmdPprof = "pprof"
|
|
|
|
cmdMetrics = "metrics"
|
|
|
|
cmdWallet = "wallet"
|
|
|
|
cmdAddress = "address"
|
|
|
|
cmdConfig = "config"
|
2023-02-03 09:46:25 +00:00
|
|
|
cmdConfigDir = "config-dir"
|
2022-11-24 14:49:21 +00:00
|
|
|
cmdListenAddress = "listen_address"
|
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`)
|
2023-02-03 09:46:25 +00:00
|
|
|
flags.StringArray(cmdConfig, nil, "config paths")
|
|
|
|
flags.String(cmdConfigDir, "", "config dir path")
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout")
|
2022-11-15 15:06:53 +00:00
|
|
|
flags.Duration(cfgStreamTimeout, defaultStreamTimeout, "gRPC individual message timeout")
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout")
|
|
|
|
flags.Duration(cfgRebalance, defaultRebalanceTimer, "gRPC connection rebalance timer")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
flags.String(cmdListenAddress, "0.0.0.0:8080", "addresses to listen")
|
|
|
|
flags.String(cfgTLSCertFile, "", "TLS certificate path")
|
|
|
|
flags.String(cfgTLSKeyFile, "", "TLS key path")
|
2022-12-16 08:45:27 +00:00
|
|
|
peers := flags.StringArrayP(cfgPeers, "p", nil, "FrostFS 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)
|
|
|
|
}
|
|
|
|
|
2022-08-01 20:52:07 +00:00
|
|
|
if err := v.BindPFlag(cfgWalletPath, flags.Lookup(cmdWallet)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.BindPFlag(cfgWalletAddress, flags.Lookup(cmdAddress)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if err := v.BindPFlags(flags); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
if err := v.BindPFlag(cfgServer+".0.address", flags.Lookup(cmdListenAddress)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgServer+".0."+cfgTLSKeyFile, flags.Lookup(cfgTLSKeyFile)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgServer+".0."+cfgTLSCertFile, flags.Lookup(cfgTLSCertFile)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if err := flags.Parse(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
if v.IsSet(cfgServer+".0."+cfgTLSKeyFile) && v.IsSet(cfgServer+".0."+cfgTLSCertFile) {
|
|
|
|
v.Set(cfgServer+".0."+cfgTLSEnabled, true)
|
|
|
|
}
|
|
|
|
|
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:
|
2022-12-16 08:45:27 +00:00
|
|
|
fmt.Printf("FrostFS 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
|
|
|
|
}
|
|
|
|
|
2022-08-01 20:47:20 +00:00
|
|
|
defaultValue := v.GetString(keys[i])
|
|
|
|
if len(defaultValue) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-27 15:18:53 +00:00
|
|
|
k := strings.Replace(keys[i], ".", "_", -1)
|
2022-08-01 20:47:20 +00:00
|
|
|
fmt.Printf("%s_%s = %s\n", Prefix, strings.ToUpper(k), defaultValue)
|
2020-11-27 15:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-16 08:45:27 +00:00
|
|
|
fmt.Printf("FrostFS HTTP Gateway\nVersion: %s\nGoVersion: %s\n", Version, runtime.Version())
|
2019-12-13 16:02:48 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2023-02-03 09:46:25 +00:00
|
|
|
if err := readInConfig(v); err != nil {
|
|
|
|
panic(err)
|
2021-06-28 10:24:46 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-09-08 14:57:22 +00:00
|
|
|
|
2023-02-03 09:46:25 +00:00
|
|
|
func readInConfig(v *viper.Viper) error {
|
|
|
|
if v.IsSet(cmdConfig) {
|
|
|
|
if err := readConfig(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet(cmdConfigDir) {
|
|
|
|
if err := readConfigDir(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfigDir(v *viper.Viper) error {
|
|
|
|
cfgSubConfigDir := v.GetString(cmdConfigDir)
|
|
|
|
entries, err := os.ReadDir(cfgSubConfigDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ext := path.Ext(entry.Name())
|
|
|
|
if ext != ".yaml" && ext != ".yml" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = mergeConfig(v, path.Join(cfgSubConfigDir, entry.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func readConfig(v *viper.Viper) error {
|
2023-02-03 09:46:25 +00:00
|
|
|
for _, fileName := range v.GetStringSlice(cmdConfig) {
|
|
|
|
if err := mergeConfig(v, fileName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeConfig(v *viper.Viper, fileName string) error {
|
|
|
|
cfgFile, err := os.Open(fileName)
|
2022-09-08 14:57:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-03 09:46:25 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if errClose := cfgFile.Close(); errClose != nil {
|
|
|
|
panic(errClose)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = v.MergeConfig(cfgFile); err != nil {
|
2022-09-08 14:57:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-03 09:46:25 +00:00
|
|
|
return nil
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newLogger constructs a zap.Logger instance for current application.
|
|
|
|
// Panics on failure.
|
|
|
|
//
|
|
|
|
// Logger is built from zap's production logging configuration with:
|
|
|
|
// - parameterized level (debug by default)
|
|
|
|
// - console encoding
|
|
|
|
// - ISO8601 time encoding
|
|
|
|
//
|
|
|
|
// Logger records a stack trace for all messages at or above fatal level.
|
|
|
|
//
|
|
|
|
// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace.
|
|
|
|
func newLogger(v *viper.Viper) (*zap.Logger, zap.AtomicLevel) {
|
|
|
|
lvl, err := getLogLevel(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := zap.NewProductionConfig()
|
|
|
|
c.Level = zap.NewAtomicLevelAt(lvl)
|
|
|
|
c.Encoding = "console"
|
|
|
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
|
|
|
|
l, err := c.Build(
|
|
|
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("build zap logger instance: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, c.Level
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogLevel(v *viper.Viper) (zapcore.Level, error) {
|
|
|
|
var lvl zapcore.Level
|
|
|
|
lvlStr := v.GetString(cfgLoggerLevel)
|
|
|
|
err := lvl.UnmarshalText([]byte(lvlStr))
|
|
|
|
if err != nil {
|
|
|
|
return lvl, fmt.Errorf("incorrect logger level configuration %s (%v), "+
|
|
|
|
"value should be one of %v", lvlStr, err, [...]zapcore.Level{
|
|
|
|
zapcore.DebugLevel,
|
|
|
|
zapcore.InfoLevel,
|
|
|
|
zapcore.WarnLevel,
|
|
|
|
zapcore.ErrorLevel,
|
|
|
|
zapcore.DPanicLevel,
|
|
|
|
zapcore.PanicLevel,
|
|
|
|
zapcore.FatalLevel,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return lvl, nil
|
|
|
|
}
|
2022-11-24 14:49:21 +00:00
|
|
|
|
|
|
|
func fetchServers(v *viper.Viper) []ServerInfo {
|
|
|
|
var servers []ServerInfo
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
key := cfgServer + "." + strconv.Itoa(i) + "."
|
|
|
|
|
|
|
|
var serverInfo ServerInfo
|
|
|
|
serverInfo.Address = v.GetString(key + "address")
|
|
|
|
serverInfo.TLS.Enabled = v.GetBool(key + cfgTLSEnabled)
|
|
|
|
serverInfo.TLS.KeyFile = v.GetString(key + cfgTLSKeyFile)
|
|
|
|
serverInfo.TLS.CertFile = v.GetString(key + cfgTLSCertFile)
|
|
|
|
|
|
|
|
if serverInfo.Address == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
servers = append(servers, serverInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|