Migrate to new release of NeoFS API

- migrate to new release NeoFS API / CDN SDK
- add `Last-Modified` header

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-11-23 12:32:03 +03:00
parent f14e2b695e
commit a4f7a2f962
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2
4 changed files with 45 additions and 23 deletions

21
app.go
View file

@ -6,7 +6,6 @@ import (
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"strconv" "strconv"
"time"
"github.com/fasthttp/router" "github.com/fasthttp/router"
sdk "github.com/nspcc-dev/cdn-neofs-sdk" sdk "github.com/nspcc-dev/cdn-neofs-sdk"
@ -35,9 +34,6 @@ type (
jobDone chan struct{} jobDone chan struct{}
webDone chan struct{} webDone chan struct{}
conTimeout time.Duration
reqTimeout time.Duration
} }
App interface { App interface {
@ -87,8 +83,9 @@ func newApp(ctx context.Context, opt ...Option) App {
grpclog.SetLoggerV2(a.wlog) grpclog.SetLoggerV2(a.wlog)
} }
a.conTimeout = a.cfg.GetDuration("connect_timeout") conTimeout := a.cfg.GetDuration("connect_timeout")
a.reqTimeout = a.cfg.GetDuration("request_timeout") reqTimeout := a.cfg.GetDuration("request_timeout")
tckTimeout := a.cfg.GetDuration("rebalance_timer")
// -- setup FastHTTP server: -- // -- setup FastHTTP server: --
a.web.Name = "neofs-http-gate" a.web.Name = "neofs-http-gate"
@ -111,6 +108,9 @@ func newApp(ctx context.Context, opt ...Option) App {
} }
connections[address] = weight 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) 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.WithLogger(a.log),
pool.WithCredentials(cred), pool.WithCredentials(cred),
pool.WithWeightPool(connections), pool.WithWeightPool(connections),
pool.WithTickerTimeout(tckTimeout),
pool.WithConnectTimeout(conTimeout),
pool.WithRequestTimeout(reqTimeout),
pool.WithAPIPreparer(sdk.APIPreparer),
pool.WithGRPCOptions( pool.WithGRPCOptions(
grpc.WithBlock(), grpc.WithBlock(),
grpc.WithInsecure(), grpc.WithInsecure(),
@ -138,7 +142,8 @@ func newApp(ctx context.Context, opt ...Option) App {
a.cli, err = sdk.New(ctx, a.cli, err = sdk.New(ctx,
sdk.WithLogger(a.log), sdk.WithLogger(a.log),
sdk.WithCredentials(cred), sdk.WithCredentials(cred),
sdk.WithConnectionPool(a.pool)) sdk.WithConnectionPool(a.pool),
sdk.WithAPIPreparer(sdk.APIPreparer))
if err != nil { if err != nil {
a.log.Fatal("could not prepare sdk client", zap.Error(err)) 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) { func prepareCredentials(key string, log *zap.Logger) (neofs.Credentials, error) {
if key == generated { if key == generated {
log.Fatal("Don't use generated key, deprecated")
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil { if err != nil {
return nil, err return nil, err

6
go.mod
View file

@ -4,8 +4,8 @@ go 1.13
require ( require (
github.com/fasthttp/router v1.1.6 github.com/fasthttp/router v1.1.6
github.com/nspcc-dev/cdn-neofs-sdk v0.0.0-20201113175725-f448ffe8047c github.com/nspcc-dev/cdn-neofs-sdk v0.0.0
github.com/nspcc-dev/neofs-api-go v0.0.0-20201029071528-352e99d9b91a github.com/nspcc-dev/neofs-api-go v1.20.2
github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/neofs-crypto v0.3.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.6.0 github.com/prometheus/client_golang v1.6.0
@ -16,3 +16,5 @@ require (
go.uber.org/zap v1.16.0 go.uber.org/zap v1.16.0
google.golang.org/grpc v1.33.1 google.golang.org/grpc v1.33.1
) )
replace github.com/nspcc-dev/cdn-neofs-sdk => ../sdk

6
go.sum
View file

@ -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 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= 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/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-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-20191209120240-0d6b7568d9ae/go.mod h1:3FjXOoHmA51EGfb5GS/HOv7VdmngNRTssSeQ729dvGY=
github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk= 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.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 h1:KKOPMKs0fm8JIau1SuwxiLdrZ+1kDPBiVRlWwzfebWE=
github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc= 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 v1.20.2 h1:jOsfgOQdKhg7gTAGciRdIdOJxYX72CxqgkwaqzEscHw=
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/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.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.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM=

View file

@ -89,19 +89,34 @@ func (a *app) receiveFile(c *fasthttp.RequestCtx) {
disp = "attachment" disp = "attachment"
} }
c.Response.Header.Set("Content-Length", strconv.FormatUint(obj.GetPayloadSize(), 10)) c.Response.Header.Set("Content-Length", strconv.FormatUint(obj.PayloadSize(), 10))
c.Response.Header.Set("x-object-id", obj.GetID().String()) c.Response.Header.Set("x-object-id", obj.ID().String())
c.Response.Header.Set("x-owner-id", obj.GetOwnerID().String()) c.Response.Header.Set("x-owner-id", obj.OwnerID().String())
c.Response.Header.Set("x-container-id", obj.GetContainerID().String()) c.Response.Header.Set("x-container-id", obj.ContainerID().String())
for _, attr := range obj.GetAttributes() { for _, attr := range obj.Attributes() {
key := attr.GetKey() key := attr.Key()
val := attr.GetValue() val := attr.Value()
if key == object.AttributeFileName {
filename = val
}
c.Response.Header.Set("x-"+key, val) 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) c.SetContentType(writer.contentType)