2020-07-06 09:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-19 01:05:28 +00:00
|
|
|
"errors"
|
2020-07-12 23:00:47 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-10-19 01:05:28 +00:00
|
|
|
"os"
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2021-01-14 17:39:48 +00:00
|
|
|
sdk "github.com/nspcc-dev/cdn-sdk"
|
|
|
|
"github.com/nspcc-dev/cdn-sdk/creds/hcs"
|
|
|
|
"github.com/nspcc-dev/cdn-sdk/creds/neofs"
|
|
|
|
"github.com/nspcc-dev/cdn-sdk/pool"
|
2020-08-06 12:02:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
2020-11-24 07:09:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/auth"
|
2020-08-06 12:02:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/handler"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/layer"
|
2020-07-06 09:18:16 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
2020-11-24 07:09:58 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-07-07 11:25:13 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
App struct {
|
2020-11-24 07:09:58 +00:00
|
|
|
cli pool.Client
|
|
|
|
ctr auth.Center
|
2020-10-13 09:31:23 +00:00
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
tls *tlsConfig
|
|
|
|
obj layer.Client
|
|
|
|
api api.Handler
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
maxClients api.MaxClients
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
webDone chan struct{}
|
|
|
|
wrkDone chan struct{}
|
|
|
|
}
|
2020-07-13 15:50:11 +00:00
|
|
|
|
|
|
|
tlsConfig struct {
|
|
|
|
KeyFile string
|
|
|
|
CertFile string
|
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
func newApp(ctx context.Context, l *zap.Logger, v *viper.Viper) *App {
|
2020-07-06 09:18:16 +00:00
|
|
|
var (
|
2020-10-13 09:31:23 +00:00
|
|
|
err error
|
|
|
|
tls *tlsConfig
|
2020-11-24 07:09:58 +00:00
|
|
|
cli sdk.Client
|
|
|
|
con pool.Client
|
2020-10-13 09:31:23 +00:00
|
|
|
caller api.Handler
|
2020-11-24 07:09:58 +00:00
|
|
|
ctr auth.Center
|
2020-10-13 09:31:23 +00:00
|
|
|
obj layer.Client
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
hcsCred hcs.Credentials
|
|
|
|
nfsCred neofs.Credentials
|
|
|
|
|
|
|
|
peers = fetchPeers(l, v)
|
2020-10-13 09:31:23 +00:00
|
|
|
|
2020-07-15 20:16:27 +00:00
|
|
|
reBalance = defaultRebalanceTimer
|
2020-07-06 09:18:16 +00:00
|
|
|
conTimeout = defaultConnectTimeout
|
|
|
|
reqTimeout = defaultRequestTimeout
|
2020-07-22 13:02:32 +00:00
|
|
|
|
|
|
|
maxClientsCount = defaultMaxClientsCount
|
|
|
|
maxClientsDeadline = defaultMaxClientsDeadline
|
2020-11-24 07:09:58 +00:00
|
|
|
|
|
|
|
hcsCredential = v.GetString(cfgGateAuthPrivateKey)
|
|
|
|
nfsCredential = v.GetString(cfgNeoFSPrivateKey)
|
2020-07-06 09:18:16 +00:00
|
|
|
)
|
2020-07-13 15:50:11 +00:00
|
|
|
|
2020-07-07 11:25:13 +00:00
|
|
|
if v := v.GetDuration(cfgConnectTimeout); v > 0 {
|
2020-07-06 09:18:16 +00:00
|
|
|
conTimeout = v
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:25:13 +00:00
|
|
|
if v := v.GetDuration(cfgRequestTimeout); v > 0 {
|
2020-07-06 09:18:16 +00:00
|
|
|
reqTimeout = v
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
if v := v.GetInt(cfgMaxClientsCount); v > 0 {
|
|
|
|
maxClientsCount = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := v.GetDuration(cfgMaxClientsDeadline); v > 0 {
|
|
|
|
maxClientsDeadline = v
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:31:23 +00:00
|
|
|
if v := v.GetDuration(cfgRebalanceTimer); v > 0 {
|
|
|
|
reBalance = v
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
if nfsCred, err = neofs.New(nfsCredential); err != nil {
|
2020-10-13 09:31:23 +00:00
|
|
|
l.Fatal("could not load NeoFS private key")
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
if hcsCred, err = hcs.NewCredentials(hcsCredential); err != nil {
|
2020-10-13 09:31:23 +00:00
|
|
|
l.Fatal("could not load gate auth key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet(cfgTLSKeyFile) && v.IsSet(cfgTLSCertFile) {
|
|
|
|
tls = &tlsConfig{
|
|
|
|
KeyFile: v.GetString(cfgTLSKeyFile),
|
|
|
|
CertFile: v.GetString(cfgTLSCertFile),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
l.Info("using credentials",
|
|
|
|
zap.String("HCS", hcsCredential),
|
|
|
|
zap.String("NeoFS", nfsCredential))
|
|
|
|
|
|
|
|
poolOptions := []pool.Option{
|
|
|
|
pool.WithLogger(l),
|
|
|
|
pool.WithWeightPool(peers),
|
|
|
|
pool.WithCredentials(nfsCred),
|
|
|
|
pool.WithTickerTimeout(reBalance),
|
|
|
|
pool.WithConnectTimeout(conTimeout),
|
|
|
|
pool.WithRequestTimeout(reqTimeout),
|
|
|
|
pool.WithAPIPreparer(sdk.APIPreparer),
|
|
|
|
pool.WithGRPCOptions(
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: v.GetDuration(cfgKeepaliveTime),
|
|
|
|
Timeout: v.GetDuration(cfgKeepaliveTimeout),
|
|
|
|
PermitWithoutStream: v.GetBool(cfgKeepalivePermitWithoutStream),
|
|
|
|
}))}
|
|
|
|
|
|
|
|
if con, err = pool.New(ctx, poolOptions...); err != nil {
|
2020-07-15 20:16:27 +00:00
|
|
|
l.Fatal("could not prepare pool connections", zap.Error(err))
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // should establish connection with NeoFS Storage Nodes
|
2020-10-13 09:31:23 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, conTimeout)
|
2020-07-06 09:18:16 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
if _, err = con.Connection(ctx); err != nil {
|
2020-10-19 01:05:28 +00:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
l.Info("connection canceled")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
l.Fatal("could not establish connection",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
if cli, err = sdk.New(ctx,
|
|
|
|
sdk.WithLogger(l),
|
|
|
|
sdk.WithConnectionPool(con),
|
|
|
|
sdk.WithCredentials(nfsCred),
|
|
|
|
sdk.WithAPIPreparer(sdk.APIPreparer)); err != nil {
|
|
|
|
l.Fatal("could not prepare sdk client",
|
|
|
|
zap.Error(err))
|
2020-10-19 01:05:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 07:09:58 +00:00
|
|
|
// prepare object layer
|
|
|
|
obj = layer.NewLayer(l, cli)
|
|
|
|
|
|
|
|
// prepare auth center
|
2020-11-24 16:31:57 +00:00
|
|
|
ctr = auth.New(cli.Object(), hcsCred.PrivateKey())
|
2020-07-06 09:18:16 +00:00
|
|
|
|
2020-08-06 10:50:04 +00:00
|
|
|
if caller, err = handler.New(l, obj); err != nil {
|
|
|
|
l.Fatal("could not initialize API handler", zap.Error(err))
|
2020-07-24 16:10:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
return &App{
|
2020-10-13 09:31:23 +00:00
|
|
|
ctr: ctr,
|
2020-11-24 07:09:58 +00:00
|
|
|
cli: con,
|
2020-10-13 09:31:23 +00:00
|
|
|
log: l,
|
|
|
|
cfg: v,
|
|
|
|
obj: obj,
|
|
|
|
tls: tls,
|
|
|
|
api: caller,
|
2020-07-06 09:18:16 +00:00
|
|
|
|
|
|
|
webDone: make(chan struct{}, 1),
|
|
|
|
wrkDone: make(chan struct{}, 1),
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
maxClients: api.NewMaxClientsMiddleware(maxClientsCount, maxClientsDeadline),
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 23:00:47 +00:00
|
|
|
func (a *App) Wait() {
|
2020-07-06 09:18:16 +00:00
|
|
|
a.log.Info("application started")
|
2020-07-12 23:00:47 +00:00
|
|
|
|
2020-07-06 09:18:16 +00:00
|
|
|
select {
|
|
|
|
case <-a.wrkDone: // wait for worker is stopped
|
|
|
|
<-a.webDone
|
|
|
|
case <-a.webDone: // wait for web-server is stopped
|
|
|
|
<-a.wrkDone
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
a.log.Info("application finished")
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Server(ctx context.Context) {
|
2020-07-12 23:00:47 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
lis net.Listener
|
|
|
|
lic net.ListenConfig
|
2020-07-13 09:51:21 +00:00
|
|
|
srv = new(http.Server)
|
2020-07-12 23:00:47 +00:00
|
|
|
addr = a.cfg.GetString(cfgListenAddress)
|
|
|
|
)
|
|
|
|
|
|
|
|
if lis, err = lic.Listen(ctx, "tcp", addr); err != nil {
|
|
|
|
a.log.Fatal("could not prepare listener",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:51:21 +00:00
|
|
|
router := newS3Router()
|
|
|
|
|
2020-07-12 23:00:47 +00:00
|
|
|
// Attach app-specific routes:
|
2020-07-13 09:51:21 +00:00
|
|
|
attachHealthy(router, a.cli)
|
|
|
|
attachMetrics(router, a.cfg, a.log)
|
|
|
|
attachProfiler(router, a.cfg, a.log)
|
|
|
|
|
|
|
|
// Attach S3 API:
|
2020-12-10 15:11:45 +00:00
|
|
|
domains := fetchDomains(a.cfg)
|
|
|
|
a.log.Info("fetch domains, prepare to use API",
|
|
|
|
zap.Strings("domains", domains))
|
|
|
|
api.Attach(router, domains, a.maxClients, a.api, a.ctr, a.log)
|
2020-07-13 09:51:21 +00:00
|
|
|
|
|
|
|
// Use mux.Router as http.Handler
|
|
|
|
srv.Handler = router
|
2020-10-23 00:12:37 +00:00
|
|
|
srv.ErrorLog = zap.NewStdLog(a.log)
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
a.log.Info("starting server",
|
|
|
|
zap.String("bind", addr))
|
|
|
|
|
2020-07-13 15:50:11 +00:00
|
|
|
switch a.tls {
|
|
|
|
case nil:
|
|
|
|
if err = srv.Serve(lis); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.log.Fatal("listen and serve",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
a.log.Info("using certificate",
|
|
|
|
zap.String("key", a.tls.KeyFile),
|
|
|
|
zap.String("cert", a.tls.CertFile))
|
2020-07-13 11:23:23 +00:00
|
|
|
|
2020-07-13 15:50:11 +00:00
|
|
|
if err = srv.ServeTLS(lis, a.tls.CertFile, a.tls.KeyFile); err != nil && err != http.ErrServerClosed {
|
|
|
|
a.log.Fatal("listen and serve",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
2020-07-12 23:00:47 +00:00
|
|
|
}
|
2020-07-06 09:18:16 +00:00
|
|
|
}()
|
2020-07-12 23:00:47 +00:00
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
a.log.Info("stopping server",
|
|
|
|
zap.Error(srv.Shutdown(ctx)))
|
|
|
|
|
|
|
|
close(a.webDone)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Worker(ctx context.Context) {
|
2020-11-24 07:09:58 +00:00
|
|
|
a.cli.Worker(ctx)
|
2020-07-12 23:00:47 +00:00
|
|
|
a.log.Info("stopping worker")
|
|
|
|
close(a.wrkDone)
|
2020-07-06 09:18:16 +00:00
|
|
|
}
|