2020-03-31 08:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-07 12:54:30 +00:00
|
|
|
"math"
|
2020-11-09 13:43:23 +00:00
|
|
|
"strconv"
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
"github.com/fasthttp/router"
|
2021-04-05 14:48:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/connections"
|
2021-03-31 18:24:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/downloader"
|
2021-03-30 22:46:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/logger"
|
|
|
|
"github.com/nspcc-dev/neofs-http-gate/neofs"
|
2021-03-31 16:58:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/uploader"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
2020-07-02 08:34:54 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
app struct {
|
2021-03-31 19:08:39 +00:00
|
|
|
log *zap.Logger
|
|
|
|
plant neofs.ClientPlant
|
|
|
|
cfg *viper.Viper
|
|
|
|
auxiliaryLog logger.Logger
|
|
|
|
webServer *fasthttp.Server
|
|
|
|
webDone chan struct{}
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
App interface {
|
|
|
|
Wait()
|
|
|
|
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 {
|
2021-04-14 19:57:58 +00:00
|
|
|
var (
|
|
|
|
creds neofs.Credentials
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-03-31 08:37:10 +00:00
|
|
|
a := &app{
|
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)
|
|
|
|
}
|
2021-03-31 18:24:41 +00:00
|
|
|
a.auxiliaryLog = logger.GRPC(a.log)
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdVerbose) {
|
2021-03-31 18:24:41 +00:00
|
|
|
grpclog.SetLoggerV2(a.auxiliaryLog)
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- setup FastHTTP server --
|
2021-03-31 19:08:39 +00:00
|
|
|
a.webServer.Name = "neofs-http-gate"
|
|
|
|
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
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2021-04-14 19:57:58 +00:00
|
|
|
keystring := a.cfg.GetString(cmdNeoFSKey)
|
|
|
|
if len(keystring) == 0 {
|
|
|
|
a.log.Info("no key specified, creating one automatically for this run")
|
|
|
|
creds, err = neofs.NewEphemeralCredentials()
|
|
|
|
} else {
|
|
|
|
creds, err = neofs.NewCredentials(keystring)
|
|
|
|
}
|
2021-04-05 14:48:01 +00:00
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("failed to get neofs credentials", zap.Error(err))
|
|
|
|
}
|
|
|
|
pb := new(connections.PoolBuilder)
|
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")
|
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-04-05 14:48:01 +00:00
|
|
|
pb.AddNode(address, weight)
|
2021-03-31 19:08:39 +00:00
|
|
|
a.log.Info("add connection", zap.String("address", address), zap.Float64("weight", weight))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2021-04-05 14:48:01 +00:00
|
|
|
opts := &connections.PoolBuilderOptions{
|
|
|
|
Key: creds.PrivateKey(),
|
|
|
|
NodeConnectionTimeout: a.cfg.GetDuration(cfgConTimeout),
|
|
|
|
NodeRequestTimeout: a.cfg.GetDuration(cfgReqTimeout),
|
|
|
|
ClientRebalanceInterval: a.cfg.GetDuration(cfgRebalance),
|
2021-04-07 12:54:30 +00:00
|
|
|
SessionExpirationEpoch: math.MaxUint64,
|
2021-04-29 19:58:54 +00:00
|
|
|
KeepaliveTime: a.cfg.GetDuration(cfgKeepaliveTime),
|
|
|
|
KeepaliveTimeout: a.cfg.GetDuration(cfgKeepaliveTimeout),
|
|
|
|
KeepalivePermitWoStream: a.cfg.GetBool(cfgKeepalivePermitWithoutStream),
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
pool, err := pb.Build(ctx, opts)
|
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
|
|
|
}
|
2021-04-05 14:48:01 +00:00
|
|
|
a.plant, err = neofs.NewClientPlant(ctx, pool, creds)
|
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 neofs client plant")
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2020-03-31 08:37:10 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Wait() {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("starting application")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Serve(ctx context.Context) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
2021-03-31 19:08:39 +00:00
|
|
|
a.log.Info("shutting down web server", zap.Error(a.webServer.Shutdown()))
|
2020-03-31 08:37:10 +00:00
|
|
|
close(a.webDone)
|
|
|
|
}()
|
2021-03-31 19:08:39 +00:00
|
|
|
edts := a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
|
|
|
|
uploader := uploader.New(a.log, a.plant, edts)
|
2021-03-31 18:24:41 +00:00
|
|
|
downloader, err := downloader.New(ctx, a.log, a.plant)
|
|
|
|
if err != nil {
|
|
|
|
a.log.Fatal("failed to create downloader", zap.Error(err))
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
// Configure router.
|
2020-03-31 08:37:10 +00:00
|
|
|
r := router.New()
|
|
|
|
r.RedirectTrailingSlash = true
|
2021-03-31 16:58:42 +00:00
|
|
|
r.POST("/upload/{cid}", uploader.Upload)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /upload/{cid}")
|
2021-03-31 18:24:41 +00:00
|
|
|
r.GET("/get/{cid}/{oid}", downloader.DownloadByAddress)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /get/{cid}/{oid}")
|
2021-03-31 18:24:41 +00:00
|
|
|
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", downloader.DownloadByAttribute)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /get_by_attribute/{cid}/{attr_key}/{attr_val:*}")
|
2020-03-31 08:37:10 +00:00
|
|
|
// enable metrics
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdMetrics) {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /metrics/")
|
2021-03-31 18:24:41 +00:00
|
|
|
attachMetrics(r, a.auxiliaryLog)
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
// enable pprof
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdPprof) {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /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)
|
2021-04-15 14:12:24 +00:00
|
|
|
tlsCertPath := a.cfg.GetString(cfgTLSCertificate)
|
|
|
|
tlsKeyPath := a.cfg.GetString(cfgTLSKey)
|
|
|
|
|
2021-03-31 19:08:39 +00:00
|
|
|
a.webServer.Handler = r.Handler
|
2021-04-15 14:12:24 +00:00
|
|
|
if tlsCertPath == "" && tlsKeyPath == "" {
|
|
|
|
a.log.Info("running web server", zap.String("address", bind))
|
|
|
|
err = a.webServer.ListenAndServe(bind)
|
|
|
|
} else {
|
|
|
|
a.log.Info("running web server (TLS-enabled)", zap.String("address", bind))
|
|
|
|
err = a.webServer.ListenAndServeTLS(bind, tlsCertPath, tlsKeyPath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-03-31 08:37:10 +00:00
|
|
|
a.log.Fatal("could not start server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|