forked from TrueCloudLab/frostfs-http-gw
NSPCC-493 Separate repository for NeoFS GW
- Update dependencies - Update README (configuration: flags section) - Add gRPC logger and flag to enable gRPC connection debugging - Refactored settings (add flags, use defaults instead of yaml representation)
This commit is contained in:
parent
2bebf38a18
commit
97d1a99f58
8 changed files with 425 additions and 209 deletions
178
main.go
178
main.go
|
@ -2,65 +2,27 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"crypto/ecdsa"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/nspcc-dev/neofs-proto/object"
|
||||
"github.com/nspcc-dev/neofs-proto/refs"
|
||||
"github.com/nspcc-dev/neofs-proto/service"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
log *zap.Logger
|
||||
timeout time.Duration
|
||||
key *ecdsa.PrivateKey
|
||||
cli object.ServiceClient
|
||||
}
|
||||
|
||||
var defaultConfig = strings.NewReader(`
|
||||
request_timeout: 5s
|
||||
connect_timeout: 30s
|
||||
listen_address: :8082
|
||||
neofs_node_addr: :8080
|
||||
|
||||
logger:
|
||||
level: debug
|
||||
format: console
|
||||
trace_level: fatal
|
||||
no_disclaimer: true
|
||||
sampling:
|
||||
initial: 1000
|
||||
thereafter: 1000
|
||||
|
||||
keepalive:
|
||||
time: 100ms
|
||||
timeout: 10s
|
||||
permit_without_stream: true
|
||||
`)
|
||||
|
||||
func main() {
|
||||
v := viper.New()
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvPrefix("GW")
|
||||
v.SetConfigType("yaml")
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
// set defaults:
|
||||
v.Set("app.name", "neofs-gw")
|
||||
v.Set("app.version", Version)
|
||||
|
||||
if err := v.ReadConfig(defaultConfig); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
v := settings()
|
||||
|
||||
log, err := newLogger(v)
|
||||
if err != nil {
|
||||
|
@ -74,12 +36,20 @@ func main() {
|
|||
grace = newGracefulContext(log)
|
||||
)
|
||||
|
||||
if v.GetBool("verbose") {
|
||||
grpclog.SetLoggerV2(
|
||||
gRPCLogger(log))
|
||||
}
|
||||
|
||||
cfg.log = log
|
||||
cfg.key = fetchKey(log, v)
|
||||
cfg.timeout = v.GetDuration("request_timeout")
|
||||
|
||||
ctx, cancel := context.WithTimeout(grace, v.GetDuration("connect_timeout"))
|
||||
defer cancel()
|
||||
|
||||
conn, err := grpc.DialContext(ctx, v.GetString("neofs_node_addr"),
|
||||
conn, err := grpc.DialContext(ctx, v.GetString("neofs_address"),
|
||||
grpc.WithBlock(),
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: v.GetDuration("keepalive.time"),
|
||||
|
@ -105,9 +75,11 @@ func main() {
|
|||
e.HideBanner = true
|
||||
|
||||
e.GET("/:cid/:oid", cfg.receiveFile)
|
||||
|
||||
go func() {
|
||||
log.Info("run gateway server",
|
||||
zap.String("address", v.GetString("listen_address")))
|
||||
|
||||
if err := e.Start(v.GetString("listen_address")); err != nil {
|
||||
log.Panic("could not start server", zap.Error(err))
|
||||
}
|
||||
|
@ -118,9 +90,7 @@ func main() {
|
|||
ctx, cancel = context.WithTimeout(context.TODO(), time.Second*30)
|
||||
defer cancel()
|
||||
|
||||
if err := e.Shutdown(ctx); err != nil {
|
||||
log.Panic("could not stop server", zap.Error(err))
|
||||
}
|
||||
log.Info("stopping server", zap.Error(e.Shutdown(ctx)))
|
||||
}
|
||||
|
||||
func checkConnection(ctx context.Context, conn *grpc.ClientConn, log *zap.Logger) {
|
||||
|
@ -135,7 +105,7 @@ loop:
|
|||
case connectivity.Idle, connectivity.Connecting, connectivity.Ready:
|
||||
// It's ok..
|
||||
default:
|
||||
log.Panic("could not establish connection",
|
||||
log.Error("could not establish connection",
|
||||
zap.Stringer("state", state),
|
||||
zap.Any("connection", conn.Target()))
|
||||
}
|
||||
|
@ -144,117 +114,3 @@ loop:
|
|||
|
||||
tick.Stop()
|
||||
}
|
||||
|
||||
func (cfg *config) receiveFile(c echo.Context) error {
|
||||
var (
|
||||
cid refs.CID
|
||||
oid refs.ObjectID
|
||||
obj *object.Object
|
||||
download = c.QueryParam("download") != ""
|
||||
)
|
||||
|
||||
cfg.log.Debug("try to fetch object from network",
|
||||
zap.String("cid", c.Param("cid")),
|
||||
zap.String("oid", c.Param("oid")))
|
||||
|
||||
if err := cid.Parse(c.Param("cid")); err != nil {
|
||||
cfg.log.Error("wrong container id",
|
||||
zap.String("cid", c.Param("cid")),
|
||||
zap.String("oid", c.Param("oid")),
|
||||
zap.Error(err))
|
||||
|
||||
return echo.NewHTTPError(
|
||||
http.StatusBadRequest,
|
||||
errors.Wrap(err, "wrong container id").Error(),
|
||||
)
|
||||
} else if err := oid.Parse(c.Param("oid")); err != nil {
|
||||
cfg.log.Error("wrong object id",
|
||||
zap.Stringer("cid", cid),
|
||||
zap.String("oid", c.Param("oid")),
|
||||
zap.Error(err))
|
||||
|
||||
return echo.NewHTTPError(
|
||||
http.StatusBadRequest,
|
||||
errors.Wrap(err, "wrong object id").Error(),
|
||||
)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.timeout)
|
||||
defer cancel()
|
||||
|
||||
cli, err := cfg.cli.Get(ctx, &object.GetRequest{
|
||||
Address: refs.Address{ObjectID: oid, CID: cid},
|
||||
TTL: service.SingleForwardingTTL,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
cfg.log.Error("could not prepare connection",
|
||||
zap.Stringer("cid", cid),
|
||||
zap.Stringer("oid", oid),
|
||||
zap.Error(err))
|
||||
|
||||
return echo.NewHTTPError(
|
||||
// TODO: nginx doesn't return 500 errors from backend
|
||||
// http.StatusInternalServerError,
|
||||
http.StatusBadRequest,
|
||||
errors.Wrap(err, "could not prepare connection").Error(),
|
||||
)
|
||||
} else if obj, err = receiveObject(cli); err != nil {
|
||||
cfg.log.Error("could not receive object",
|
||||
zap.Stringer("cid", cid),
|
||||
zap.Stringer("oid", oid),
|
||||
zap.Error(err))
|
||||
|
||||
if strings.Contains(err.Error(), object.ErrNotFound.Error()) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(
|
||||
// TODO: nginx doesn't return 500 errors from backend
|
||||
// http.StatusInternalServerError,
|
||||
http.StatusBadRequest,
|
||||
errors.Wrap(err, "could not receive object").Error(),
|
||||
)
|
||||
}
|
||||
|
||||
cfg.log.Info("object fetched successfully",
|
||||
zap.Stringer("cid", cid),
|
||||
zap.Stringer("oid", oid))
|
||||
|
||||
c.Response().Header().Set("Content-Length", strconv.FormatUint(obj.SystemHeader.PayloadLength, 10))
|
||||
c.Response().Header().Set("x-object-id", obj.SystemHeader.ID.String())
|
||||
c.Response().Header().Set("x-owner-id", obj.SystemHeader.OwnerID.String())
|
||||
c.Response().Header().Set("x-container-id", obj.SystemHeader.CID.String())
|
||||
|
||||
for i := range obj.Headers {
|
||||
if hdr := obj.Headers[i].GetUserHeader(); hdr != nil {
|
||||
c.Response().Header().Set("x-"+hdr.Key, hdr.Value)
|
||||
|
||||
if hdr.Key == DropInFilenameHeader && download {
|
||||
c.Response().Header().Set("Content-Disposition", "attachment; filename="+hdr.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK,
|
||||
http.DetectContentType(obj.Payload),
|
||||
obj.Payload)
|
||||
}
|
||||
|
||||
func receiveObject(cli object.Service_GetClient) (*object.Object, error) {
|
||||
var obj *object.Object
|
||||
for {
|
||||
resp, err := cli.Recv()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
} else if obj == nil {
|
||||
obj = resp.GetObject()
|
||||
}
|
||||
|
||||
obj.Payload = append(obj.Payload, resp.GetChunk()...)
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue