*: fix all comment-related golint warnings
Some of this code is going to be moved to SDK library, so it's important. Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
parent
3173c70eb6
commit
df3c87af79
14 changed files with 88 additions and 4 deletions
4
app.go
4
app.go
|
@ -27,14 +27,17 @@ type (
|
|||
webDone chan struct{}
|
||||
}
|
||||
|
||||
// App is an interface for the main gateway function.
|
||||
App interface {
|
||||
Wait()
|
||||
Serve(context.Context)
|
||||
}
|
||||
|
||||
// Option is an application option.
|
||||
Option func(a *app)
|
||||
)
|
||||
|
||||
// WithLogger returns Option to set a specific logger.
|
||||
func WithLogger(l *zap.Logger) Option {
|
||||
return func(a *app) {
|
||||
if l == nil {
|
||||
|
@ -44,6 +47,7 @@ func WithLogger(l *zap.Logger) Option {
|
|||
}
|
||||
}
|
||||
|
||||
// WithConfig returns Option to use specific Viper configuration.
|
||||
func WithConfig(c *viper.Viper) Option {
|
||||
return func(a *app) {
|
||||
if c == nil {
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
// PoolBuilderOptions contains options used to build connection pool.
|
||||
type PoolBuilderOptions struct {
|
||||
Key *ecdsa.PrivateKey
|
||||
NodeConnectionTimeout time.Duration
|
||||
|
@ -28,17 +29,21 @@ type PoolBuilderOptions struct {
|
|||
connections []*grpc.ClientConn
|
||||
}
|
||||
|
||||
// PoolBuilder is an interim structure used to collect node addresses/weights and
|
||||
// build connection pool subsequently.
|
||||
type PoolBuilder struct {
|
||||
addresses []string
|
||||
weights []float64
|
||||
}
|
||||
|
||||
// AddNode adds address/weight pair to node PoolBuilder list.
|
||||
func (pb *PoolBuilder) AddNode(address string, weight float64) *PoolBuilder {
|
||||
pb.addresses = append(pb.addresses, address)
|
||||
pb.weights = append(pb.weights, weight)
|
||||
return pb
|
||||
}
|
||||
|
||||
// Build creates new pool based on current PoolBuilder state and options.
|
||||
func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (Pool, error) {
|
||||
if len(pb.addresses) == 0 {
|
||||
return nil, errors.New("no NeoFS peers configured")
|
||||
|
@ -75,6 +80,7 @@ func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (
|
|||
return new(ctx, options)
|
||||
}
|
||||
|
||||
// Pool is an interface providing connection artifacts on request.
|
||||
type Pool interface {
|
||||
ConnectionArtifacts() (client.Client, *token.SessionToken, error)
|
||||
}
|
||||
|
|
|
@ -2,13 +2,17 @@ package connections
|
|||
|
||||
import "math/rand"
|
||||
|
||||
// See Vose's Alias Method (https://www.keithschwarz.com/darts-dice-coins/).
|
||||
// Sampler implements weighted random number generation using Vose's Alias
|
||||
// Method (https://www.keithschwarz.com/darts-dice-coins/).
|
||||
type Sampler struct {
|
||||
randomGenerator *rand.Rand
|
||||
probabilities []float64
|
||||
alias []int
|
||||
}
|
||||
|
||||
// NewSampler creates new Sampler with a given set of probabilities using
|
||||
// given source of randomness. Created Sampler will produce numbers from
|
||||
// 0 to len(probabilities).
|
||||
func NewSampler(probabilities []float64, source rand.Source) *Sampler {
|
||||
sampler := &Sampler{}
|
||||
var (
|
||||
|
@ -53,6 +57,7 @@ func NewSampler(probabilities []float64, source rand.Source) *Sampler {
|
|||
return sampler
|
||||
}
|
||||
|
||||
// Next returns the next (not so) random number from Sampler.
|
||||
func (g *Sampler) Next() int {
|
||||
n := len(g.alias)
|
||||
i := g.randomGenerator.Intn(n)
|
||||
|
|
|
@ -165,11 +165,13 @@ func (o objectIDs) Slice() []string {
|
|||
return res
|
||||
}
|
||||
|
||||
// Downloader is a download request handler.
|
||||
type Downloader struct {
|
||||
log *zap.Logger
|
||||
plant neofs.ClientPlant
|
||||
}
|
||||
|
||||
// New creates an instance of Downloader using specified options.
|
||||
func New(ctx context.Context, log *zap.Logger, plant neofs.ClientPlant) (*Downloader, error) {
|
||||
var err error
|
||||
d := &Downloader{log: log, plant: plant}
|
||||
|
@ -187,6 +189,7 @@ func (d *Downloader) newRequest(ctx *fasthttp.RequestCtx, log *zap.Logger) *requ
|
|||
}
|
||||
}
|
||||
|
||||
// DownloadByAddress handles download requests using simple cid/oid format.
|
||||
func (d *Downloader) DownloadByAddress(c *fasthttp.RequestCtx) {
|
||||
var (
|
||||
err error
|
||||
|
@ -214,6 +217,7 @@ func (d *Downloader) DownloadByAddress(c *fasthttp.RequestCtx) {
|
|||
d.newRequest(c, log).receiveFile(getOpts)
|
||||
}
|
||||
|
||||
// DownloadByAttribute handles attribute-based download requests.
|
||||
func (d *Downloader) DownloadByAttribute(c *fasthttp.RequestCtx) {
|
||||
var (
|
||||
err error
|
||||
|
|
|
@ -12,6 +12,8 @@ var (
|
|||
globalContextOnce sync.Once
|
||||
)
|
||||
|
||||
// Context returns global context with initialized INT, TERM and HUP signal
|
||||
// handlers set to notify this context.
|
||||
func Context() context.Context {
|
||||
globalContextOnce.Do(func() {
|
||||
globalContext, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||
|
|
|
@ -12,12 +12,15 @@ type (
|
|||
log *zap.SugaredLogger
|
||||
}
|
||||
|
||||
// Logger includes grpclog.LoggerV2 interface with an additional
|
||||
// Println method.
|
||||
Logger interface {
|
||||
grpclog.LoggerV2
|
||||
Println(v ...interface{})
|
||||
}
|
||||
)
|
||||
|
||||
// GRPC wraps given zap.Logger into grpclog.LoggerV2+ interface.
|
||||
func GRPC(l *zap.Logger) Logger {
|
||||
log := l.WithOptions(
|
||||
// skip gRPCLog + zapLogger in caller
|
||||
|
@ -29,32 +32,47 @@ func GRPC(l *zap.Logger) Logger {
|
|||
}
|
||||
}
|
||||
|
||||
// Info implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Info(args ...interface{}) { z.log.Info(args...) }
|
||||
|
||||
// Infoln implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Infoln(args ...interface{}) { z.log.Info(args...) }
|
||||
|
||||
// Infof implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Infof(format string, args ...interface{}) { z.log.Infof(format, args...) }
|
||||
|
||||
// Println allows to print a line with info severity.
|
||||
func (z *zapLogger) Println(args ...interface{}) { z.log.Info(args...) }
|
||||
|
||||
// Printf implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Printf(format string, args ...interface{}) { z.log.Infof(format, args...) }
|
||||
|
||||
// Warning implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Warning(args ...interface{}) { z.log.Warn(args...) }
|
||||
|
||||
// Warningln implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Warningln(args ...interface{}) { z.log.Warn(args...) }
|
||||
|
||||
// Warningf implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Warningf(format string, args ...interface{}) { z.log.Warnf(format, args...) }
|
||||
|
||||
// Error implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Error(args ...interface{}) { z.log.Error(args...) }
|
||||
|
||||
// Errorln implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Errorln(args ...interface{}) { z.log.Error(args...) }
|
||||
|
||||
// Errorf implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Errorf(format string, args ...interface{}) { z.log.Errorf(format, args...) }
|
||||
|
||||
// Fatal implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Fatal(args ...interface{}) { z.log.Fatal(args...) }
|
||||
|
||||
// Fatalln implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Fatalln(args ...interface{}) { z.log.Fatal(args...) }
|
||||
|
||||
// Fatalf implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) Fatalf(format string, args ...interface{}) { z.log.Fatalf(format, args...) }
|
||||
|
||||
// V implements grpclog.LoggerV2.
|
||||
func (z *zapLogger) V(int) bool { return z.Enabled(zapcore.DebugLevel) }
|
||||
|
|
|
@ -2,22 +2,32 @@ package logger
|
|||
|
||||
import "go.uber.org/zap"
|
||||
|
||||
// WithSamplingInitial returns Option that sets sampling initial parameter.
|
||||
func WithSamplingInitial(v int) Option { return func(o *options) { o.SamplingInitial = v } }
|
||||
|
||||
// WithSamplingThereafter returns Option that sets sampling thereafter parameter.
|
||||
func WithSamplingThereafter(v int) Option { return func(o *options) { o.SamplingThereafter = v } }
|
||||
|
||||
// WithFormat returns Option that sets format parameter.
|
||||
func WithFormat(v string) Option { return func(o *options) { o.Format = v } }
|
||||
|
||||
// WithLevel returns Option that sets Level parameter.
|
||||
func WithLevel(v string) Option { return func(o *options) { o.Level = v } }
|
||||
|
||||
// WithTraceLevel returns Option that sets trace level parameter.
|
||||
func WithTraceLevel(v string) Option { return func(o *options) { o.TraceLevel = v } }
|
||||
|
||||
// WithoutDisclaimer returns Option that disables disclaimer.
|
||||
func WithoutDisclaimer() Option { return func(o *options) { o.NoDisclaimer = true } }
|
||||
|
||||
// WithoutCaller returns Option that disables caller printing.
|
||||
func WithoutCaller() Option { return func(o *options) { o.NoCaller = true } }
|
||||
|
||||
// WithAppName returns Option that sets application name.
|
||||
func WithAppName(v string) Option { return func(o *options) { o.AppName = v } }
|
||||
|
||||
// WithAppVersion returns Option that sets application version.
|
||||
func WithAppVersion(v string) Option { return func(o *options) { o.AppVersion = v } }
|
||||
|
||||
// WithZapOptions returns Option that sets zap logger options.
|
||||
func WithZapOptions(opts ...zap.Option) Option { return func(o *options) { o.Options = opts } }
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
)
|
||||
|
||||
type (
|
||||
// Option represents logger option setter.
|
||||
Option func(o *options)
|
||||
|
||||
options struct {
|
||||
|
@ -77,6 +78,8 @@ func defaults() *options {
|
|||
}
|
||||
}
|
||||
|
||||
// New returns new zap.Logger using all options specified and stdout used
|
||||
// for output.
|
||||
func New(opts ...Option) (*zap.Logger, error) {
|
||||
o := defaults()
|
||||
c := zap.NewProductionConfig()
|
||||
|
|
4
misc.go
4
misc.go
|
@ -1,8 +1,12 @@
|
|||
package main
|
||||
|
||||
// Prefix is a prefix used for environment variables containing gateway
|
||||
// configuration.
|
||||
const Prefix = "HTTP_GW"
|
||||
|
||||
var (
|
||||
// Build is a timestamp set during gateway build.
|
||||
Build = "now"
|
||||
// Version is gateway version.
|
||||
Version = "dev"
|
||||
)
|
||||
|
|
|
@ -13,12 +13,14 @@ import (
|
|||
"github.com/nspcc-dev/neofs-http-gate/connections"
|
||||
)
|
||||
|
||||
// BaseOptions represents basic NeoFS request options.
|
||||
type BaseOptions struct {
|
||||
Client client.Client
|
||||
SessionToken *token.SessionToken
|
||||
BearerToken *token.BearerToken
|
||||
}
|
||||
|
||||
// PutOptions represents NeoFS Put request options.
|
||||
type PutOptions struct {
|
||||
BaseOptions
|
||||
Attributes []*object.Attribute
|
||||
|
@ -27,12 +29,14 @@ type PutOptions struct {
|
|||
Reader io.Reader
|
||||
}
|
||||
|
||||
// GetOptions represents NeoFS Get request options.
|
||||
type GetOptions struct {
|
||||
BaseOptions
|
||||
ObjectAddress *object.Address
|
||||
Writer io.Writer
|
||||
}
|
||||
|
||||
// SearchOptions represents NeoFS Search request options.
|
||||
type SearchOptions struct {
|
||||
BaseOptions
|
||||
ContainerID *container.ID
|
||||
|
@ -42,11 +46,13 @@ type SearchOptions struct {
|
|||
}
|
||||
}
|
||||
|
||||
// DeleteOptions represents NeoFS Delete request options.
|
||||
type DeleteOptions struct {
|
||||
BaseOptions
|
||||
ObjectAddress *object.Address
|
||||
}
|
||||
|
||||
// ObjectClient wraps basic NeoFS requests.
|
||||
type ObjectClient interface {
|
||||
Put(context.Context, *PutOptions) (*object.Address, error)
|
||||
Get(context.Context, *GetOptions) (*object.Object, error)
|
||||
|
@ -54,6 +60,8 @@ type ObjectClient interface {
|
|||
Delete(context.Context, *DeleteOptions) error
|
||||
}
|
||||
|
||||
// ClientPlant provides connections to NeoFS nodes from pool and allows to
|
||||
// get local owner ID.
|
||||
type ClientPlant interface {
|
||||
ConnectionArtifacts() (client.Client, *token.SessionToken, error)
|
||||
Object() ObjectClient
|
||||
|
@ -71,10 +79,12 @@ type neofsClientPlant struct {
|
|||
pool connections.Pool
|
||||
}
|
||||
|
||||
// ConnectionArtifacts returns connection from pool.
|
||||
func (cp *neofsClientPlant) ConnectionArtifacts() (client.Client, *token.SessionToken, error) {
|
||||
return cp.pool.ConnectionArtifacts()
|
||||
}
|
||||
|
||||
// Object returns ObjectClient instance from plant.
|
||||
func (cp *neofsClientPlant) Object() ObjectClient {
|
||||
return &neofsObjectClient{
|
||||
key: cp.key,
|
||||
|
@ -82,14 +92,17 @@ func (cp *neofsClientPlant) Object() ObjectClient {
|
|||
}
|
||||
}
|
||||
|
||||
// OwnerID returns plant's owner ID.
|
||||
func (cp *neofsClientPlant) OwnerID() *owner.ID {
|
||||
return cp.ownerID
|
||||
}
|
||||
|
||||
// NewClientPlant creates new ClientPlant from given context, pool and credentials.
|
||||
func NewClientPlant(ctx context.Context, pool connections.Pool, creds Credentials) (ClientPlant, error) {
|
||||
return &neofsClientPlant{key: creds.PrivateKey(), ownerID: creds.Owner(), pool: pool}, nil
|
||||
}
|
||||
|
||||
// Put does NeoFS Put request, returning new object address if successful.
|
||||
func (oc *neofsObjectClient) Put(ctx context.Context, options *PutOptions) (*object.Address, error) {
|
||||
var (
|
||||
err error
|
||||
|
@ -117,6 +130,7 @@ func (oc *neofsObjectClient) Put(ctx context.Context, options *PutOptions) (*obj
|
|||
return address, nil
|
||||
}
|
||||
|
||||
// Get does NeoFS Get request, returning an object received if successful.
|
||||
func (oc *neofsObjectClient) Get(ctx context.Context, options *GetOptions) (*object.Object, error) {
|
||||
var (
|
||||
err error
|
||||
|
@ -134,6 +148,7 @@ func (oc *neofsObjectClient) Get(ctx context.Context, options *GetOptions) (*obj
|
|||
return obj, err
|
||||
}
|
||||
|
||||
// Search does NeoFS Search request, returning object IDs if successful.
|
||||
func (oc *neofsObjectClient) Search(ctx context.Context, options *SearchOptions) ([]*object.ID, error) {
|
||||
sfs := object.NewSearchFilters()
|
||||
sfs.AddRootFilter()
|
||||
|
@ -149,6 +164,7 @@ func (oc *neofsObjectClient) Search(ctx context.Context, options *SearchOptions)
|
|||
)
|
||||
}
|
||||
|
||||
// Delete deletes NeoFS object.
|
||||
func (oc *neofsObjectClient) Delete(ctx context.Context, options *DeleteOptions) error {
|
||||
ops := new(client.DeleteObjectParams).WithAddress(options.ObjectAddress)
|
||||
err := options.Client.DeleteObject(
|
||||
|
|
|
@ -24,8 +24,8 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
// New creates an instance of Credentials through string representation of secret.
|
||||
// It allows passing WIF, path, hex-encoded and others.
|
||||
// NewCredentials creates an instance of Credentials through string
|
||||
// representation of secret. It allows passing WIF, path, hex-encoded and others.
|
||||
func NewCredentials(secret string) (Credentials, error) {
|
||||
key, err := crypto.LoadPrivateKey(secret)
|
||||
if err != nil {
|
||||
|
|
|
@ -26,6 +26,7 @@ const (
|
|||
// return
|
||||
// }
|
||||
|
||||
// BearerTokenFromHeader extracts bearer token from Authorization request header.
|
||||
func BearerTokenFromHeader(h *fasthttp.RequestHeader) []byte {
|
||||
auth := h.Peek(fasthttp.HeaderAuthorization)
|
||||
if auth == nil || !bytes.HasPrefix(auth, []byte(bearerTokenHdr)) {
|
||||
|
@ -37,6 +38,7 @@ func BearerTokenFromHeader(h *fasthttp.RequestHeader) []byte {
|
|||
return auth
|
||||
}
|
||||
|
||||
// BearerTokenFromCookie extracts bearer token from cookies.
|
||||
func BearerTokenFromCookie(h *fasthttp.RequestHeader) []byte {
|
||||
auth := h.Cookie(bearerTokenHdr)
|
||||
if len(auth) == 0 {
|
||||
|
@ -46,6 +48,8 @@ func BearerTokenFromCookie(h *fasthttp.RequestHeader) []byte {
|
|||
return auth
|
||||
}
|
||||
|
||||
// StoreBearerToken extracts bearer token from header or cookie and stores
|
||||
// it in the request context.
|
||||
func StoreBearerToken(ctx *fasthttp.RequestCtx) error {
|
||||
tkn, err := fetchBearerToken(ctx)
|
||||
if err != nil {
|
||||
|
@ -56,6 +60,8 @@ func StoreBearerToken(ctx *fasthttp.RequestCtx) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// LoadBearerToken returns bearer token stored in context given (if it's
|
||||
// present there).
|
||||
func LoadBearerToken(ctx context.Context) (*token.BearerToken, error) {
|
||||
if tkn, ok := ctx.Value(bearerTokenKey).(*token.BearerToken); ok && tkn != nil {
|
||||
return tkn, nil
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// MultipartFile provides standard ReadCloser interface and also allows one to
|
||||
// get file name, it's used for multipart uploads.
|
||||
type MultipartFile interface {
|
||||
io.ReadCloser
|
||||
FileName() string
|
||||
|
|
|
@ -29,16 +29,20 @@ var putOptionsPool = sync.Pool{
|
|||
},
|
||||
}
|
||||
|
||||
// Uploader is an upload request handler.
|
||||
type Uploader struct {
|
||||
log *zap.Logger
|
||||
plant neofs.ClientPlant
|
||||
enableDefaultTimestamp bool
|
||||
}
|
||||
|
||||
// New creates a new Uploader using specified logger, connection pool and
|
||||
// other options.
|
||||
func New(log *zap.Logger, plant neofs.ClientPlant, enableDefaultTimestamp bool) *Uploader {
|
||||
return &Uploader{log, plant, enableDefaultTimestamp}
|
||||
}
|
||||
|
||||
// Upload handles multipart upload request.
|
||||
func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
|
||||
var (
|
||||
err error
|
||||
|
|
Loading…
Reference in a new issue