From a4f7a2f9627181aa83c188db9a95a0a1fcc397e3 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 23 Nov 2020 12:32:03 +0300 Subject: [PATCH] Migrate to new release of NeoFS API - migrate to new release NeoFS API / CDN SDK - add `Last-Modified` header Signed-off-by: Evgeniy Kulikov --- app.go | 21 ++++++++++++++------- go.mod | 6 ++++-- go.sum | 6 ++---- receive.go | 35 +++++++++++++++++++++++++---------- 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/app.go b/app.go index c3e9f1a..b40ad53 100644 --- a/app.go +++ b/app.go @@ -6,7 +6,6 @@ import ( "crypto/elliptic" "crypto/rand" "strconv" - "time" "github.com/fasthttp/router" sdk "github.com/nspcc-dev/cdn-neofs-sdk" @@ -35,9 +34,6 @@ type ( jobDone chan struct{} webDone chan struct{} - - conTimeout time.Duration - reqTimeout time.Duration } App interface { @@ -87,8 +83,9 @@ func newApp(ctx context.Context, opt ...Option) App { grpclog.SetLoggerV2(a.wlog) } - a.conTimeout = a.cfg.GetDuration("connect_timeout") - a.reqTimeout = a.cfg.GetDuration("request_timeout") + conTimeout := a.cfg.GetDuration("connect_timeout") + reqTimeout := a.cfg.GetDuration("request_timeout") + tckTimeout := a.cfg.GetDuration("rebalance_timer") // -- setup FastHTTP server: -- a.web.Name = "neofs-http-gate" @@ -111,6 +108,9 @@ func newApp(ctx context.Context, opt ...Option) App { } connections[address] = weight + a.log.Info("add connection peer", + zap.String("address", address), + zap.Float64("weight", weight)) } cred, err := prepareCredentials(a.cfg.GetString("key"), a.log) @@ -122,6 +122,10 @@ func newApp(ctx context.Context, opt ...Option) App { pool.WithLogger(a.log), pool.WithCredentials(cred), pool.WithWeightPool(connections), + pool.WithTickerTimeout(tckTimeout), + pool.WithConnectTimeout(conTimeout), + pool.WithRequestTimeout(reqTimeout), + pool.WithAPIPreparer(sdk.APIPreparer), pool.WithGRPCOptions( grpc.WithBlock(), grpc.WithInsecure(), @@ -138,7 +142,8 @@ func newApp(ctx context.Context, opt ...Option) App { a.cli, err = sdk.New(ctx, sdk.WithLogger(a.log), sdk.WithCredentials(cred), - sdk.WithConnectionPool(a.pool)) + sdk.WithConnectionPool(a.pool), + sdk.WithAPIPreparer(sdk.APIPreparer)) if err != nil { a.log.Fatal("could not prepare sdk client", zap.Error(err)) } @@ -148,6 +153,8 @@ func newApp(ctx context.Context, opt ...Option) App { func prepareCredentials(key string, log *zap.Logger) (neofs.Credentials, error) { if key == generated { + log.Fatal("Don't use generated key, deprecated") + sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { return nil, err diff --git a/go.mod b/go.mod index fea2106..dfedabe 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.13 require ( github.com/fasthttp/router v1.1.6 - github.com/nspcc-dev/cdn-neofs-sdk v0.0.0-20201113175725-f448ffe8047c - github.com/nspcc-dev/neofs-api-go v0.0.0-20201029071528-352e99d9b91a + github.com/nspcc-dev/cdn-neofs-sdk v0.0.0 + github.com/nspcc-dev/neofs-api-go v1.20.2 github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.6.0 @@ -16,3 +16,5 @@ require ( go.uber.org/zap v1.16.0 google.golang.org/grpc v1.33.1 ) + +replace github.com/nspcc-dev/cdn-neofs-sdk => ../sdk diff --git a/go.sum b/go.sum index a21dcc6..71c40b0 100644 --- a/go.sum +++ b/go.sum @@ -220,8 +220,6 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/nspcc-dev/cdn-neofs-sdk v0.0.0-20201113175725-f448ffe8047c h1:la/q4996O/wKifXCFWQ4WKOxnQLVtaEeGJBVlQ/AUyY= -github.com/nspcc-dev/cdn-neofs-sdk v0.0.0-20201113175725-f448ffe8047c/go.mod h1:p/YXjSDO2pJ5JwYKrLwVKIxdcVStjFxD0gfzIrwy5eY= github.com/nspcc-dev/dbft v0.0.0-20191205084618-dacb1a30c254/go.mod h1:w1Ln2aT+dBlPhLnuZhBV+DfPEdS2CHWWLp5JTScY3bw= github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae/go.mod h1:3FjXOoHmA51EGfb5GS/HOv7VdmngNRTssSeQ729dvGY= github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk= @@ -232,8 +230,8 @@ github.com/nspcc-dev/hrw v1.0.9/go.mod h1:l/W2vx83vMQo6aStyx2AuZrJ+07lGv2JQGlVkP github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg= github.com/nspcc-dev/neo-go v0.91.0 h1:KKOPMKs0fm8JIau1SuwxiLdrZ+1kDPBiVRlWwzfebWE= github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc= -github.com/nspcc-dev/neofs-api-go v0.0.0-20201029071528-352e99d9b91a h1:A/8D9A9Fbp4tw1xGgfZB0vtPKEQ/LYq0l7DFuM4KeS4= -github.com/nspcc-dev/neofs-api-go v0.0.0-20201029071528-352e99d9b91a/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= +github.com/nspcc-dev/neofs-api-go v1.20.2 h1:jOsfgOQdKhg7gTAGciRdIdOJxYX72CxqgkwaqzEscHw= +github.com/nspcc-dev/neofs-api-go v1.20.2/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= diff --git a/receive.go b/receive.go index 9f8a8a5..5688b0f 100644 --- a/receive.go +++ b/receive.go @@ -89,19 +89,34 @@ func (a *app) receiveFile(c *fasthttp.RequestCtx) { disp = "attachment" } - c.Response.Header.Set("Content-Length", strconv.FormatUint(obj.GetPayloadSize(), 10)) - c.Response.Header.Set("x-object-id", obj.GetID().String()) - c.Response.Header.Set("x-owner-id", obj.GetOwnerID().String()) - c.Response.Header.Set("x-container-id", obj.GetContainerID().String()) + c.Response.Header.Set("Content-Length", strconv.FormatUint(obj.PayloadSize(), 10)) + c.Response.Header.Set("x-object-id", obj.ID().String()) + c.Response.Header.Set("x-owner-id", obj.OwnerID().String()) + c.Response.Header.Set("x-container-id", obj.ContainerID().String()) - for _, attr := range obj.GetAttributes() { - key := attr.GetKey() - val := attr.GetValue() - if key == object.AttributeFileName { - filename = val - } + for _, attr := range obj.Attributes() { + key := attr.Key() + val := attr.Value() c.Response.Header.Set("x-"+key, val) + + switch key { + case object.AttributeFileName: + filename = val + case object.AttributeTimestamp: + value, err := strconv.ParseInt(val, 10, 64) + if err != nil { + a.log.Info("couldn't parse creation date", + zap.String("key", key), + zap.String("val", val), + zap.Error(err)) + continue + } + + c.Response.Header.Set("Last-Modified", + time.Unix(value, 0).Format(time.RFC1123)) + } + } c.SetContentType(writer.contentType)