2020-03-31 08:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-22 11:53:59 +00:00
|
|
|
"fmt"
|
2022-11-24 14:49:21 +00:00
|
|
|
"net/http"
|
2022-09-08 14:57:22 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-11-09 13:43:23 +00:00
|
|
|
"strconv"
|
2022-09-08 14:57:22 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2023-03-15 08:07:44 +00:00
|
|
|
"time"
|
2020-03-31 08:37:10 +00:00
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/downloader"
|
2023-05-05 08:19:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/frostfs/services"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/metrics"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
2023-05-30 14:01:20 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tokens"
|
2023-05-05 08:19:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tree"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/uploader"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/fasthttp/router"
|
2021-06-22 11:53:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2022-04-07 12:56:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-06-22 11:53:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
2023-05-05 08:19:35 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
app struct {
|
2023-05-30 14:01:20 +00:00
|
|
|
ctx context.Context
|
2022-03-25 13:06:33 +00:00
|
|
|
log *zap.Logger
|
2022-09-08 14:57:22 +00:00
|
|
|
logLevel zap.AtomicLevel
|
2022-03-25 13:06:33 +00:00
|
|
|
pool *pool.Pool
|
2023-05-05 08:19:35 +00:00
|
|
|
key *keys.PrivateKey
|
2022-07-25 09:47:48 +00:00
|
|
|
owner *user.ID
|
2022-03-25 13:06:33 +00:00
|
|
|
cfg *viper.Viper
|
|
|
|
webServer *fasthttp.Server
|
|
|
|
webDone chan struct{}
|
2022-04-20 09:17:20 +00:00
|
|
|
resolver *resolver.ContainerResolver
|
2022-09-08 14:57:22 +00:00
|
|
|
metrics *gateMetrics
|
|
|
|
services []*metrics.Service
|
2022-09-09 06:33:31 +00:00
|
|
|
settings *appSettings
|
2022-11-24 14:49:21 +00:00
|
|
|
servers []Server
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
appSettings struct {
|
2022-11-24 14:49:21 +00:00
|
|
|
Uploader *uploader.Settings
|
|
|
|
Downloader *downloader.Settings
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// App is an interface for the main gateway function.
|
2020-03-31 08:37:10 +00:00
|
|
|
App interface {
|
|
|
|
Wait()
|
2023-05-30 14:01:20 +00:00
|
|
|
Serve()
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Option is an application option.
|
2020-03-31 08:37:10 +00:00
|
|
|
Option func(a *app)
|
2022-07-22 08:30:57 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
gateMetrics struct {
|
|
|
|
logger *zap.Logger
|
2023-04-17 13:28:27 +00:00
|
|
|
provider *metrics.GateMetrics
|
2022-09-08 14:57:22 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
enabled bool
|
|
|
|
}
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// WithLogger returns Option to set a specific logger.
|
2022-09-08 14:57:22 +00:00
|
|
|
func WithLogger(l *zap.Logger, lvl zap.AtomicLevel) Option {
|
2020-03-31 08:37:10 +00:00
|
|
|
return func(a *app) {
|
|
|
|
if l == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.log = l
|
2022-09-08 14:57:22 +00:00
|
|
|
a.logLevel = lvl
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// WithConfig returns Option to use specific Viper configuration.
|
2020-03-31 08:37:10 +00:00
|
|
|
func WithConfig(c *viper.Viper) Option {
|
|
|
|
return func(a *app) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.cfg = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
func newApp(ctx context.Context, opt ...Option) App {
|
2021-04-14 19:57:58 +00:00
|
|
|
var (
|
2021-05-28 20:24:04 +00:00
|
|
|
err error
|
2021-04-14 19:57:58 +00:00
|
|
|
)
|
|
|
|
|
2020-03-31 08:37:10 +00:00
|
|
|
a := &app{
|
2023-05-30 14:01:20 +00:00
|
|
|
ctx: ctx,
|
2021-03-31 19:08:39 +00:00
|
|
|
log: zap.L(),
|
|
|
|
cfg: viper.GetViper(),
|
|
|
|
webServer: new(fasthttp.Server),
|
|
|
|
webDone: make(chan struct{}),
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
for i := range opt {
|
|
|
|
opt[i](a)
|
|
|
|
}
|
2022-03-25 13:06:33 +00:00
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- setup FastHTTP server --
|
2022-12-16 08:45:27 +00:00
|
|
|
a.webServer.Name = "frost-http-gw"
|
2021-03-31 19:08:39 +00:00
|
|
|
a.webServer.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
|
|
|
a.webServer.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
|
|
|
|
a.webServer.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
|
|
|
a.webServer.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
|
|
|
a.webServer.DisableHeaderNamesNormalizing = true
|
|
|
|
a.webServer.NoDefaultServerHeader = true
|
|
|
|
a.webServer.NoDefaultContentType = true
|
|
|
|
a.webServer.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize)
|
|
|
|
a.webServer.DisablePreParseMultipartForm = true
|
|
|
|
a.webServer.StreamRequestBody = a.cfg.GetBool(cfgWebStreamRequestBody)
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2023-05-05 08:19:35 +00:00
|
|
|
a.key, err = getFrostFSKey(a)
|
2021-04-05 14:48:01 +00:00
|
|
|
if err != nil {
|
2022-12-20 11:01:50 +00:00
|
|
|
a.log.Fatal("failed to get frostfs credentials", zap.Error(err))
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
var owner user.ID
|
2023-05-05 08:19:35 +00:00
|
|
|
user.IDFromKey(&owner, a.key.PrivateKey.PublicKey)
|
2022-07-25 09:47:48 +00:00
|
|
|
a.owner = &owner
|
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
var prm pool.InitParameters
|
2023-05-05 08:19:35 +00:00
|
|
|
prm.SetKey(&a.key.PrivateKey)
|
2022-04-07 12:56:18 +00:00
|
|
|
prm.SetNodeDialTimeout(a.cfg.GetDuration(cfgConTimeout))
|
2022-11-15 15:06:53 +00:00
|
|
|
prm.SetNodeStreamTimeout(a.cfg.GetDuration(cfgStreamTimeout))
|
2022-04-07 12:56:18 +00:00
|
|
|
prm.SetHealthcheckTimeout(a.cfg.GetDuration(cfgReqTimeout))
|
|
|
|
prm.SetClientRebalanceInterval(a.cfg.GetDuration(cfgRebalance))
|
2022-07-29 06:34:26 +00:00
|
|
|
prm.SetErrorThreshold(a.cfg.GetUint32(cfgPoolErrorThreshold))
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
for i := 0; ; i++ {
|
2020-12-02 09:09:46 +00:00
|
|
|
address := a.cfg.GetString(cfgPeers + "." + strconv.Itoa(i) + ".address")
|
|
|
|
weight := a.cfg.GetFloat64(cfgPeers + "." + strconv.Itoa(i) + ".weight")
|
2021-12-07 11:57:16 +00:00
|
|
|
priority := a.cfg.GetInt(cfgPeers + "." + strconv.Itoa(i) + ".priority")
|
2020-11-09 13:43:23 +00:00
|
|
|
if address == "" {
|
|
|
|
break
|
|
|
|
}
|
2021-04-14 20:57:45 +00:00
|
|
|
if weight <= 0 { // unspecified or wrong
|
|
|
|
weight = 1
|
|
|
|
}
|
2021-12-07 11:57:16 +00:00
|
|
|
if priority <= 0 { // unspecified or wrong
|
|
|
|
priority = 1
|
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
prm.AddNode(pool.NewNodeParam(priority, address, weight))
|
2021-12-07 11:57:16 +00:00
|
|
|
a.log.Info("add connection", zap.String("address", address),
|
|
|
|
zap.Float64("weight", weight), zap.Int("priority", priority))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
|
|
|
|
a.pool, err = pool.NewPool(prm)
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
2021-04-05 14:48:01 +00:00
|
|
|
a.log.Fatal("failed to create connection pool", zap.Error(err))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
|
|
|
|
err = a.pool.Dial(ctx)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("failed to dial pool", zap.Error(err))
|
|
|
|
}
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2022-09-09 06:33:31 +00:00
|
|
|
a.initAppSettings()
|
2022-09-08 16:00:22 +00:00
|
|
|
a.initResolver()
|
|
|
|
a.initMetrics()
|
2023-03-15 08:07:44 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-08 16:00:22 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2022-09-09 06:33:31 +00:00
|
|
|
func (a *app) initAppSettings() {
|
|
|
|
a.settings = &appSettings{
|
2022-11-24 14:49:21 +00:00
|
|
|
Uploader: &uploader.Settings{},
|
|
|
|
Downloader: &downloader.Settings{},
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a.updateSettings()
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
func (a *app) initResolver() {
|
|
|
|
var err error
|
|
|
|
a.resolver, err = resolver.NewContainerResolver(a.getResolverConfig())
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("failed to create resolver", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) getResolverConfig() ([]string, *resolver.Config) {
|
2022-04-20 09:17:20 +00:00
|
|
|
resolveCfg := &resolver.Config{
|
2022-12-20 11:01:50 +00:00
|
|
|
FrostFS: resolver.NewFrostFSResolver(a.pool),
|
2022-04-20 09:17:20 +00:00
|
|
|
RPCAddress: a.cfg.GetString(cfgRPCEndpoint),
|
|
|
|
}
|
|
|
|
|
|
|
|
order := a.cfg.GetStringSlice(cfgResolveOrder)
|
|
|
|
if resolveCfg.RPCAddress == "" {
|
|
|
|
order = remove(order, resolver.NNSResolver)
|
|
|
|
a.log.Warn(fmt.Sprintf("resolver '%s' won't be used since '%s' isn't provided", resolver.NNSResolver, cfgRPCEndpoint))
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
if len(order) == 0 {
|
|
|
|
a.log.Info("container resolver will be disabled because of resolvers 'resolver_order' is empty")
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
return order, resolveCfg
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (a *app) initMetrics() {
|
|
|
|
gateMetricsProvider := metrics.NewGateMetrics(a.pool)
|
|
|
|
a.metrics = newGateMetrics(a.log, gateMetricsProvider, a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-04-17 13:28:27 +00:00
|
|
|
a.metrics.SetHealth(metrics.HealthStatusStarting)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:28:27 +00:00
|
|
|
func newGateMetrics(logger *zap.Logger, provider *metrics.GateMetrics, enabled bool) *gateMetrics {
|
2022-09-08 14:57:22 +00:00
|
|
|
if !enabled {
|
|
|
|
logger.Warn("metrics are disabled")
|
|
|
|
}
|
|
|
|
return &gateMetrics{
|
|
|
|
logger: logger,
|
|
|
|
provider: provider,
|
2023-04-26 06:51:37 +00:00
|
|
|
enabled: enabled,
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *gateMetrics) SetEnabled(enabled bool) {
|
|
|
|
if !enabled {
|
|
|
|
m.logger.Warn("metrics are disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
m.enabled = enabled
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-04-17 13:28:27 +00:00
|
|
|
func (m *gateMetrics) SetHealth(status metrics.HealthStatus) {
|
2022-09-08 14:57:22 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
if !m.enabled {
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
|
|
|
|
m.provider.SetHealth(status)
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:14:31 +00:00
|
|
|
func (m *gateMetrics) SetVersion(ver string) {
|
|
|
|
m.mu.RLock()
|
|
|
|
if !m.enabled {
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
|
|
|
|
m.provider.SetVersion(ver)
|
|
|
|
}
|
|
|
|
|
2022-09-09 06:57:48 +00:00
|
|
|
func (m *gateMetrics) Shutdown() {
|
|
|
|
m.mu.Lock()
|
|
|
|
if m.enabled {
|
2023-04-17 13:28:27 +00:00
|
|
|
m.provider.SetHealth(metrics.HealthStatusShuttingDown)
|
2022-09-09 06:57:48 +00:00
|
|
|
m.enabled = false
|
|
|
|
}
|
|
|
|
m.provider.Unregister()
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
func remove(list []string, element string) []string {
|
|
|
|
for i, item := range list {
|
|
|
|
if item == element {
|
|
|
|
return append(list[:i], list[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
func getFrostFSKey(a *app) (*keys.PrivateKey, error) {
|
2022-08-01 20:52:07 +00:00
|
|
|
walletPath := a.cfg.GetString(cfgWalletPath)
|
2022-04-08 09:01:57 +00:00
|
|
|
|
2021-06-22 11:53:59 +00:00
|
|
|
if len(walletPath) == 0 {
|
|
|
|
a.log.Info("no wallet path specified, creating ephemeral key automatically for this run")
|
2022-04-07 12:56:18 +00:00
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-05 08:19:35 +00:00
|
|
|
return key, nil
|
2021-06-22 11:53:59 +00:00
|
|
|
}
|
|
|
|
w, err := wallet.NewWalletFromFile(walletPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var password *string
|
|
|
|
if a.cfg.IsSet(cfgWalletPassphrase) {
|
|
|
|
pwd := a.cfg.GetString(cfgWalletPassphrase)
|
|
|
|
password = &pwd
|
|
|
|
}
|
2022-04-08 09:01:57 +00:00
|
|
|
|
2022-08-01 20:52:07 +00:00
|
|
|
address := a.cfg.GetString(cfgWalletAddress)
|
2022-04-08 09:01:57 +00:00
|
|
|
|
|
|
|
return getKeyFromWallet(w, address, password)
|
2021-06-22 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
func getKeyFromWallet(w *wallet.Wallet, addrStr string, password *string) (*keys.PrivateKey, error) {
|
2021-06-22 11:53:59 +00:00
|
|
|
var addr util.Uint160
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if addrStr == "" {
|
|
|
|
addr = w.GetChangeAddress()
|
|
|
|
} else {
|
|
|
|
addr, err = flags.ParseAddress(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid address")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
return nil, fmt.Errorf("couldn't find wallet account for %s", addrStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if password == nil {
|
|
|
|
pwd, err := input.ReadPassword("Enter password > ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't read password")
|
|
|
|
}
|
|
|
|
password = &pwd
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.Decrypt(*password, w.Scrypt); err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't decrypt account: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
return acc.PrivateKey(), nil
|
2021-06-22 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:37:10 +00:00
|
|
|
func (a *app) Wait() {
|
2022-12-16 08:45:27 +00:00
|
|
|
a.log.Info("starting application", zap.String("app_name", "frostfs-http-gw"), zap.String("version", Version))
|
2022-09-08 14:57:22 +00:00
|
|
|
|
2023-04-07 15:14:31 +00:00
|
|
|
a.metrics.SetVersion(Version)
|
2022-09-08 14:57:22 +00:00
|
|
|
a.setHealthStatus()
|
2022-07-22 08:30:57 +00:00
|
|
|
|
2021-04-15 14:16:44 +00:00
|
|
|
<-a.webDone // wait for web-server to be stopped
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (a *app) setHealthStatus() {
|
2023-04-17 13:28:27 +00:00
|
|
|
a.metrics.SetHealth(metrics.HealthStatusReady)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:01:20 +00:00
|
|
|
func (a *app) Serve() {
|
|
|
|
treeClient := a.initTree(a.ctx)
|
|
|
|
uploadRoutes := uploader.New(a.AppParams(), a.settings.Uploader)
|
|
|
|
downloadRoutes := downloader.New(a.AppParams(), a.settings.Downloader, treeClient)
|
2022-09-08 14:57:22 +00:00
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// Configure router.
|
2022-09-08 14:57:22 +00:00
|
|
|
a.configureRouter(uploadRoutes, downloadRoutes)
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
a.startServices()
|
2023-05-30 14:01:20 +00:00
|
|
|
a.initServers(a.ctx)
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
for i := range a.servers {
|
|
|
|
go func(i int) {
|
|
|
|
a.log.Info("starting server", zap.String("address", a.servers[i].Address()))
|
|
|
|
if err := a.webServer.Serve(a.servers[i].Listener()); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.log.Fatal("listen and serve", zap.Error(err))
|
2022-09-09 16:00:04 +00:00
|
|
|
}
|
2022-11-24 14:49:21 +00:00
|
|
|
}(i)
|
|
|
|
}
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGHUP)
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
2023-05-30 14:01:20 +00:00
|
|
|
case <-a.ctx.Done():
|
2022-09-08 14:57:22 +00:00
|
|
|
break LOOP
|
|
|
|
case <-sigs:
|
2023-05-30 14:01:20 +00:00
|
|
|
a.configReload(a.ctx)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
a.log.Info("shutting down web server", zap.Error(a.webServer.Shutdown()))
|
|
|
|
|
2022-09-09 06:57:48 +00:00
|
|
|
a.metrics.Shutdown()
|
2022-09-08 14:57:22 +00:00
|
|
|
a.stopServices()
|
2023-03-15 08:07:44 +00:00
|
|
|
a.shutdownTracing()
|
2022-09-08 14:57:22 +00:00
|
|
|
|
|
|
|
close(a.webDone)
|
|
|
|
}
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
func (a *app) shutdownTracing() {
|
|
|
|
const tracingShutdownTimeout = 5 * time.Second
|
|
|
|
shdnCtx, cancel := context.WithTimeout(context.Background(), tracingShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := tracing.Shutdown(shdnCtx); err != nil {
|
|
|
|
a.log.Warn("failed to shutdown tracing", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) configReload(ctx context.Context) {
|
2022-09-09 06:33:31 +00:00
|
|
|
a.log.Info("SIGHUP config reload started")
|
2023-02-03 09:46:25 +00:00
|
|
|
if !a.cfg.IsSet(cmdConfig) && !a.cfg.IsSet(cmdConfigDir) {
|
2022-09-08 14:57:22 +00:00
|
|
|
a.log.Warn("failed to reload config because it's missed")
|
|
|
|
return
|
|
|
|
}
|
2023-02-03 09:46:25 +00:00
|
|
|
if err := readInConfig(a.cfg); err != nil {
|
2022-09-08 14:57:22 +00:00
|
|
|
a.log.Warn("failed to reload config", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2023-02-03 09:46:25 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
if lvl, err := getLogLevel(a.cfg); err != nil {
|
|
|
|
a.log.Warn("log level won't be updated", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
a.logLevel.SetLevel(lvl)
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
if err := a.resolver.UpdateResolvers(a.getResolverConfig()); err != nil {
|
|
|
|
a.log.Warn("failed to update resolvers", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
if err := a.updateServers(); err != nil {
|
|
|
|
a.log.Warn("failed to reload server parameters", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
a.stopServices()
|
|
|
|
a.startServices()
|
|
|
|
|
2022-09-09 06:33:31 +00:00
|
|
|
a.updateSettings()
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
a.metrics.SetEnabled(a.cfg.GetBool(cfgPrometheusEnabled))
|
2023-03-15 08:07:44 +00:00
|
|
|
a.initTracing(ctx)
|
2022-09-08 14:57:22 +00:00
|
|
|
a.setHealthStatus()
|
2022-09-09 06:33:31 +00:00
|
|
|
|
|
|
|
a.log.Info("SIGHUP config reload completed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) updateSettings() {
|
|
|
|
a.settings.Uploader.SetDefaultTimestamp(a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp))
|
|
|
|
a.settings.Downloader.SetZipCompression(a.cfg.GetBool(cfgZipCompression))
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) startServices() {
|
|
|
|
pprofConfig := metrics.Config{Enabled: a.cfg.GetBool(cfgPprofEnabled), Address: a.cfg.GetString(cfgPprofAddress)}
|
|
|
|
pprofService := metrics.NewPprofService(a.log, pprofConfig)
|
|
|
|
a.services = append(a.services, pprofService)
|
|
|
|
go pprofService.Start()
|
|
|
|
|
|
|
|
prometheusConfig := metrics.Config{Enabled: a.cfg.GetBool(cfgPrometheusEnabled), Address: a.cfg.GetString(cfgPrometheusAddress)}
|
|
|
|
prometheusService := metrics.NewPrometheusService(a.log, prometheusConfig)
|
|
|
|
a.services = append(a.services, prometheusService)
|
|
|
|
go prometheusService.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) stopServices() {
|
2022-07-27 06:52:08 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
for _, svc := range a.services {
|
|
|
|
svc.ShutDown(ctx)
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 06:52:08 +00:00
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func (a *app) configureRouter(uploadRoutes *uploader.Uploader, downloadRoutes *downloader.Downloader) {
|
|
|
|
r := router.New()
|
|
|
|
r.RedirectTrailingSlash = true
|
|
|
|
r.NotFound = func(r *fasthttp.RequestCtx) {
|
|
|
|
response.Error(r, "Not found", fasthttp.StatusNotFound)
|
|
|
|
}
|
|
|
|
r.MethodNotAllowed = func(r *fasthttp.RequestCtx) {
|
|
|
|
response.Error(r, "Method Not Allowed", fasthttp.StatusMethodNotAllowed)
|
|
|
|
}
|
2023-05-30 14:01:20 +00:00
|
|
|
r.POST("/upload/{cid}", a.logger(a.tokenizer(a.tracer(uploadRoutes.Upload))))
|
2022-09-08 14:57:22 +00:00
|
|
|
a.log.Info("added path /upload/{cid}")
|
2023-05-30 14:01:20 +00:00
|
|
|
r.GET("/get/{cid}/{oid:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.DownloadByAddressOrBucketName))))
|
|
|
|
r.HEAD("/get/{cid}/{oid:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.HeadByAddressOrBucketName))))
|
2022-09-08 14:57:22 +00:00
|
|
|
a.log.Info("added path /get/{cid}/{oid}")
|
2023-05-30 14:01:20 +00:00
|
|
|
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.DownloadByAttribute))))
|
|
|
|
r.HEAD("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.HeadByAttribute))))
|
2022-09-08 14:57:22 +00:00
|
|
|
a.log.Info("added path /get_by_attribute/{cid}/{attr_key}/{attr_val:*}")
|
2023-05-30 14:01:20 +00:00
|
|
|
r.GET("/zip/{cid}/{prefix:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.DownloadZipped))))
|
2022-09-08 14:57:22 +00:00
|
|
|
a.log.Info("added path /zip/{cid}/{prefix}")
|
|
|
|
|
|
|
|
a.webServer.Handler = r.Handler
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
2021-07-09 09:49:08 +00:00
|
|
|
|
2023-05-30 14:01:20 +00:00
|
|
|
func (a *app) logger(req fasthttp.RequestHandler) fasthttp.RequestHandler {
|
2022-09-08 14:57:22 +00:00
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2021-07-09 09:49:08 +00:00
|
|
|
a.log.Info("request", zap.String("remote", ctx.RemoteAddr().String()),
|
|
|
|
zap.ByteString("method", ctx.Method()),
|
|
|
|
zap.ByteString("path", ctx.Path()),
|
|
|
|
zap.ByteString("query", ctx.QueryArgs().QueryString()),
|
|
|
|
zap.Uint64("id", ctx.ID()))
|
2023-05-30 14:01:20 +00:00
|
|
|
req(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) tokenizer(req fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
appCtx, err := tokens.StoreBearerTokenAppCtx(ctx, a.ctx)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Error("could not fetch and store bearer token", zap.Error(err))
|
|
|
|
response.Error(ctx, "could not fetch and store bearer token: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
}
|
|
|
|
utils.SetContextToRequest(appCtx, ctx)
|
|
|
|
req(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) tracer(req fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
appCtx := utils.GetContextFromRequest(ctx)
|
|
|
|
|
|
|
|
appCtx, span := utils.StartHTTPServerSpan(appCtx, ctx, "REQUEST")
|
|
|
|
defer func() {
|
|
|
|
utils.SetHTTPTraceInfo(appCtx, span, ctx)
|
|
|
|
span.End()
|
|
|
|
}()
|
|
|
|
|
|
|
|
utils.SetContextToRequest(appCtx, ctx)
|
|
|
|
req(ctx)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
2021-07-09 09:49:08 +00:00
|
|
|
}
|
2022-04-20 09:17:20 +00:00
|
|
|
|
|
|
|
func (a *app) AppParams() *utils.AppParams {
|
|
|
|
return &utils.AppParams{
|
|
|
|
Logger: a.log,
|
|
|
|
Pool: a.pool,
|
2022-07-25 09:47:48 +00:00
|
|
|
Owner: a.owner,
|
2022-04-20 09:17:20 +00:00
|
|
|
Resolver: a.resolver,
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 14:49:21 +00:00
|
|
|
|
|
|
|
func (a *app) initServers(ctx context.Context) {
|
|
|
|
serversInfo := fetchServers(a.cfg)
|
|
|
|
|
2023-01-18 09:44:44 +00:00
|
|
|
a.servers = make([]Server, 0, len(serversInfo))
|
|
|
|
for _, serverInfo := range serversInfo {
|
|
|
|
fields := []zap.Field{
|
2022-11-24 14:49:21 +00:00
|
|
|
zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled),
|
2023-01-18 09:44:44 +00:00
|
|
|
zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile),
|
|
|
|
}
|
|
|
|
srv, err := newServer(ctx, serverInfo)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn("failed to add server", append(fields, zap.Error(err))...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
a.servers = append(a.servers, srv)
|
|
|
|
a.log.Info("add server", fields...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.servers) == 0 {
|
|
|
|
a.log.Fatal("no healthy servers")
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) updateServers() error {
|
|
|
|
serversInfo := fetchServers(a.cfg)
|
|
|
|
|
2023-01-18 09:44:44 +00:00
|
|
|
var found bool
|
|
|
|
for _, serverInfo := range serversInfo {
|
|
|
|
index := a.serverIndex(serverInfo.Address)
|
|
|
|
if index == -1 {
|
|
|
|
continue
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if serverInfo.TLS.Enabled {
|
2023-01-18 09:44:44 +00:00
|
|
|
if err := a.servers[index].UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil {
|
2022-11-24 14:49:21 +00:00
|
|
|
return fmt.Errorf("failed to update tls certs: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 09:44:44 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("invalid servers configuration: no known server found")
|
2022-11-24 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-18 09:44:44 +00:00
|
|
|
|
|
|
|
func (a *app) serverIndex(address string) int {
|
|
|
|
for i := range a.servers {
|
|
|
|
if a.servers[i].Address() == address {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
2023-03-15 08:07:44 +00:00
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
func (a *app) initTree(ctx context.Context) *tree.Tree {
|
|
|
|
treeServiceEndpoint := a.cfg.GetString(cfgTreeServiceEndpoint)
|
|
|
|
grpcDialOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
|
|
|
|
treeGRPCClient, err := services.NewTreeServiceClientGRPC(ctx, treeServiceEndpoint, a.key, grpcDialOpt)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("failed to create tree service", zap.Error(err))
|
|
|
|
}
|
|
|
|
treeService := tree.NewTree(treeGRPCClient)
|
|
|
|
a.log.Info("init tree service", zap.String("endpoint", treeServiceEndpoint))
|
|
|
|
|
|
|
|
return treeService
|
|
|
|
}
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
func (a *app) initTracing(ctx context.Context) {
|
|
|
|
instanceID := ""
|
|
|
|
if len(a.servers) > 0 {
|
|
|
|
instanceID = a.servers[0].Address()
|
|
|
|
}
|
|
|
|
cfg := tracing.Config{
|
|
|
|
Enabled: a.cfg.GetBool(cfgTracingEnabled),
|
|
|
|
Exporter: tracing.Exporter(a.cfg.GetString(cfgTracingExporter)),
|
|
|
|
Endpoint: a.cfg.GetString(cfgTracingEndpoint),
|
|
|
|
Service: "frostfs-http-gw",
|
|
|
|
InstanceID: instanceID,
|
|
|
|
Version: Version,
|
|
|
|
}
|
|
|
|
updated, err := tracing.Setup(ctx, cfg)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Warn("failed to initialize tracing", zap.Error(err))
|
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
a.log.Info("tracing config updated")
|
|
|
|
}
|
|
|
|
}
|