2020-03-31 08:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2020-11-09 13:43:23 +00:00
|
|
|
"strconv"
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
"github.com/fasthttp/router"
|
2021-01-14 10:28:20 +00:00
|
|
|
sdk "github.com/nspcc-dev/cdn-sdk"
|
|
|
|
"github.com/nspcc-dev/cdn-sdk/creds/neofs"
|
|
|
|
"github.com/nspcc-dev/cdn-sdk/logger"
|
|
|
|
"github.com/nspcc-dev/cdn-sdk/pool"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
2020-11-09 13:43:23 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-07-02 08:34:54 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2020-11-09 13:43:23 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
app struct {
|
2020-11-09 13:43:23 +00:00
|
|
|
cli sdk.Client
|
|
|
|
pool pool.Client
|
2020-03-31 08:37:10 +00:00
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
wlog logger.Logger
|
2020-03-31 08:37:10 +00:00
|
|
|
web *fasthttp.Server
|
|
|
|
|
|
|
|
jobDone chan struct{}
|
|
|
|
webDone chan struct{}
|
2021-01-26 09:40:01 +00:00
|
|
|
|
|
|
|
enableDefaultTimestamp bool
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
App interface {
|
|
|
|
Wait()
|
|
|
|
Worker(context.Context)
|
|
|
|
Serve(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
Option func(a *app)
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithLogger(l *zap.Logger) Option {
|
|
|
|
return func(a *app) {
|
|
|
|
if l == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.log = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-03-31 08:37:10 +00:00
|
|
|
a := &app{
|
|
|
|
log: zap.L(),
|
|
|
|
cfg: viper.GetViper(),
|
|
|
|
web: new(fasthttp.Server),
|
|
|
|
|
|
|
|
jobDone: make(chan struct{}),
|
|
|
|
webDone: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opt {
|
|
|
|
opt[i](a)
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:40:01 +00:00
|
|
|
a.enableDefaultTimestamp = a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
a.wlog = logger.GRPC(a.log)
|
2020-03-31 08:37:10 +00:00
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdVerbose) {
|
2020-03-31 08:37:10 +00:00
|
|
|
grpclog.SetLoggerV2(a.wlog)
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
conTimeout := a.cfg.GetDuration(cfgConTimeout)
|
|
|
|
reqTimeout := a.cfg.GetDuration(cfgReqTimeout)
|
|
|
|
tckTimeout := a.cfg.GetDuration(cfgRebalance)
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
// -- setup FastHTTP server: --
|
|
|
|
a.web.Name = "neofs-http-gate"
|
2020-12-02 09:09:46 +00:00
|
|
|
a.web.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
|
|
|
a.web.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
|
|
|
|
a.web.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
|
|
|
a.web.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
2020-03-31 08:37:10 +00:00
|
|
|
a.web.DisableHeaderNamesNormalizing = true
|
|
|
|
a.web.NoDefaultServerHeader = true
|
|
|
|
a.web.NoDefaultContentType = true
|
2021-02-12 12:24:52 +00:00
|
|
|
a.web.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize)
|
|
|
|
|
|
|
|
// FIXME don't work with StreamRequestBody,
|
|
|
|
// some bugs with readMultipartForm
|
2021-02-12 15:48:52 +00:00
|
|
|
// https://github.com/valyala/fasthttp/issues/968
|
2021-02-12 12:24:52 +00:00
|
|
|
// a.web.DisablePreParseMultipartForm = true
|
|
|
|
|
2021-02-12 15:48:52 +00:00
|
|
|
a.web.StreamRequestBody = a.cfg.GetBool(cfgWebStreamRequestBody)
|
2020-03-31 08:37:10 +00:00
|
|
|
// -- -- -- -- -- -- -- -- -- --
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
connections := make(map[string]float64)
|
|
|
|
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")
|
2020-11-09 13:43:23 +00:00
|
|
|
if address == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
connections[address] = weight
|
2020-11-23 09:32:03 +00:00
|
|
|
a.log.Info("add connection peer",
|
|
|
|
zap.String("address", address),
|
|
|
|
zap.Float64("weight", weight))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
cred, err := neofs.New(a.cfg.GetString(cmdNeoFSKey))
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("could not prepare credentials", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
a.pool, err = pool.New(ctx,
|
|
|
|
pool.WithLogger(a.log),
|
|
|
|
pool.WithCredentials(cred),
|
|
|
|
pool.WithWeightPool(connections),
|
2020-11-23 09:32:03 +00:00
|
|
|
pool.WithTickerTimeout(tckTimeout),
|
|
|
|
pool.WithConnectTimeout(conTimeout),
|
|
|
|
pool.WithRequestTimeout(reqTimeout),
|
|
|
|
pool.WithAPIPreparer(sdk.APIPreparer),
|
2020-11-09 13:43:23 +00:00
|
|
|
pool.WithGRPCOptions(
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
2020-12-02 09:09:46 +00:00
|
|
|
Time: a.cfg.GetDuration(cfgKeepaliveTime),
|
|
|
|
Timeout: a.cfg.GetDuration(cfgKeepaliveTimeout),
|
|
|
|
PermitWithoutStream: a.cfg.GetBool(cfgKeepalivePermitWithoutStream),
|
2020-11-09 13:43:23 +00:00
|
|
|
})))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("could not prepare connection pool", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
a.cli, err = sdk.New(ctx,
|
|
|
|
sdk.WithLogger(a.log),
|
|
|
|
sdk.WithCredentials(cred),
|
2020-11-23 09:32:03 +00:00
|
|
|
sdk.WithConnectionPool(a.pool),
|
|
|
|
sdk.WithAPIPreparer(sdk.APIPreparer))
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("could not prepare sdk client", zap.Error(err))
|
|
|
|
}
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Wait() {
|
|
|
|
a.log.Info("application started")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-a.jobDone: // wait for job is stopped
|
|
|
|
<-a.webDone
|
|
|
|
case <-a.webDone: // wait for web-server is stopped
|
|
|
|
<-a.jobDone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Worker(ctx context.Context) {
|
2020-11-09 13:43:23 +00:00
|
|
|
a.pool.Worker(ctx)
|
2020-03-31 08:37:10 +00:00
|
|
|
close(a.jobDone)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Serve(ctx context.Context) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
a.log.Info("stop web-server", zap.Error(a.web.Shutdown()))
|
|
|
|
close(a.webDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
r := router.New()
|
|
|
|
r.RedirectTrailingSlash = true
|
2020-05-12 08:18:23 +00:00
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
a.log.Info("enabled /upload/{cid}")
|
|
|
|
r.POST("/upload/{cid}", a.upload)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2020-05-12 08:18:23 +00:00
|
|
|
a.log.Info("enabled /get/{cid}/{oid}")
|
2020-12-03 15:00:43 +00:00
|
|
|
r.GET("/get/{cid}/{oid}", a.byAddress)
|
2021-01-23 13:28:12 +00:00
|
|
|
|
|
|
|
a.log.Info("enabled /get_by_attribute/{cid}/{attr_key}/{attr_val:*}")
|
|
|
|
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.byAttribute)
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
// attaching /-/(ready,healthy)
|
2020-11-09 13:43:23 +00:00
|
|
|
attachHealthy(r, a.pool.Status)
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
// enable metrics
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdMetrics) {
|
2020-05-12 08:19:07 +00:00
|
|
|
a.log.Info("enabled /metrics/")
|
2020-03-31 08:37:10 +00:00
|
|
|
attachMetrics(r, a.wlog)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable pprof
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdPprof) {
|
2020-05-12 08:19:07 +00:00
|
|
|
a.log.Info("enabled /debug/pprof/")
|
2020-03-31 08:37:10 +00:00
|
|
|
attachProfiler(r)
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
bind := a.cfg.GetString(cfgListenAddress)
|
2020-03-31 08:37:10 +00:00
|
|
|
a.log.Info("run gateway server",
|
|
|
|
zap.String("address", bind))
|
|
|
|
|
|
|
|
a.web.Handler = r.Handler
|
|
|
|
if err := a.web.ListenAndServe(bind); err != nil {
|
|
|
|
a.log.Fatal("could not start server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|