frostfs-s3-gw/cmd/s3-gw/app.go

268 lines
6.0 KiB
Go
Raw Normal View History

2020-07-06 09:18:16 +00:00
package main
import (
"context"
"errors"
2020-07-12 23:00:47 +00:00
"net"
"net/http"
"os"
2020-07-06 09:18:16 +00:00
sdk "github.com/nspcc-dev/cdn-sdk"
"github.com/nspcc-dev/neofs-s3-gw/creds/hcs"
"github.com/nspcc-dev/neofs-s3-gw/creds/neofs"
"github.com/nspcc-dev/cdn-sdk/pool"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/auth"
"github.com/nspcc-dev/neofs-s3-gw/api/handler"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
2020-07-06 09:18:16 +00:00
"github.com/spf13/viper"
"go.uber.org/zap"
"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 is the main application structure.
2020-07-06 09:18:16 +00:00
App struct {
cli pool.Client
ctr auth.Center
log *zap.Logger
cfg *viper.Viper
tls *tlsConfig
obj layer.Client
api api.Handler
2020-07-06 09:18:16 +00:00
maxClients api.MaxClients
2020-07-06 09:18:16 +00:00
webDone chan struct{}
wrkDone chan struct{}
}
tlsConfig struct {
KeyFile string
CertFile string
}
2020-07-06 09:18:16 +00:00
)
func newApp(ctx context.Context, l *zap.Logger, v *viper.Viper) *App {
2020-07-06 09:18:16 +00:00
var (
err error
tls *tlsConfig
cli sdk.Client
con pool.Client
caller api.Handler
ctr auth.Center
obj layer.Client
hcsCred hcs.Credentials
nfsCred neofs.Credentials
peers = fetchPeers(l, v)
reBalance = defaultRebalanceTimer
2020-07-06 09:18:16 +00:00
conTimeout = defaultConnectTimeout
reqTimeout = defaultRequestTimeout
maxClientsCount = defaultMaxClientsCount
maxClientsDeadline = defaultMaxClientsDeadline
hcsCredential = v.GetString(cfgGateAuthPrivateKey)
nfsCredential = v.GetString(cfgNeoFSPrivateKey)
2020-07-06 09:18:16 +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
}
if v := v.GetInt(cfgMaxClientsCount); v > 0 {
maxClientsCount = v
}
if v := v.GetDuration(cfgMaxClientsDeadline); v > 0 {
maxClientsDeadline = v
}
if v := v.GetDuration(cfgRebalanceTimer); v > 0 {
reBalance = v
}
if nfsCred, err = neofs.New(nfsCredential); err != nil {
l.Fatal("could not load NeoFS private key")
}
if hcsCred, err = hcs.NewCredentials(hcsCredential); err != nil {
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),
}
}
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 {
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
ctx, cancel := context.WithTimeout(ctx, conTimeout)
2020-07-06 09:18:16 +00:00
defer cancel()
if _, err = con.Connection(ctx); err != nil {
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))
}
}
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))
}
// prepare object layer
obj = layer.NewLayer(l, cli)
// prepare auth center
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{
ctr: ctr,
cli: con,
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),
maxClients: api.NewMaxClientsMiddleware(maxClientsCount, maxClientsDeadline),
2020-07-06 09:18:16 +00:00
}
}
// Wait waits for application to finish.
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
}
// Server runs HTTP server to handle S3 API requests.
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
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))
}
router := newS3Router()
2020-07-12 23:00:47 +00:00
// Attach app-specific routes:
attachHealthy(router, a.cli)
attachMetrics(router, a.cfg, a.log)
attachProfiler(router, a.cfg, a.log)
// Attach S3 API:
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)
// Use mux.Router as http.Handler
srv.Handler = router
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))
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
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
}
// Worker runs client worker.
2020-07-06 09:18:16 +00:00
func (a *App) Worker(ctx context.Context) {
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
}