2020-03-31 08:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-30 22:46:33 +00:00
|
|
|
"sort"
|
2020-11-09 13:43:23 +00:00
|
|
|
"strconv"
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
"github.com/fasthttp/router"
|
2021-03-30 22:46:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
|
|
"github.com/nspcc-dev/neofs-http-gate/logger"
|
|
|
|
"github.com/nspcc-dev/neofs-http-gate/neofs"
|
2020-03-31 08:37:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
2020-07-02 08:34:54 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2020-03-31 08:37:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
app struct {
|
2021-03-30 22:46:33 +00:00
|
|
|
plant neofs.ClientPlant
|
|
|
|
getOperations struct {
|
|
|
|
client client.Client
|
|
|
|
sessionToken *token.SessionToken
|
|
|
|
}
|
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
wlog logger.Logger
|
|
|
|
web *fasthttp.Server
|
|
|
|
jobDone chan struct{}
|
|
|
|
webDone chan struct{}
|
2021-01-26 09:40:01 +00:00
|
|
|
enableDefaultTimestamp bool
|
2020-03-31 08:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
App interface {
|
|
|
|
Wait()
|
|
|
|
Worker(context.Context)
|
|
|
|
Serve(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
Option func(a *app)
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithLogger(l *zap.Logger) Option {
|
|
|
|
return func(a *app) {
|
|
|
|
if l == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.log = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithConfig(c *viper.Viper) Option {
|
|
|
|
return func(a *app) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.cfg = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
func newApp(ctx context.Context, opt ...Option) App {
|
2020-03-31 08:37:10 +00:00
|
|
|
a := &app{
|
|
|
|
log: zap.L(),
|
|
|
|
cfg: viper.GetViper(),
|
|
|
|
web: new(fasthttp.Server),
|
|
|
|
|
|
|
|
jobDone: make(chan struct{}),
|
|
|
|
webDone: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opt {
|
|
|
|
opt[i](a)
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:40:01 +00:00
|
|
|
a.enableDefaultTimestamp = a.cfg.GetBool(cfgUploaderHeaderEnableDefaultTimestamp)
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
a.wlog = logger.GRPC(a.log)
|
2020-03-31 08:37:10 +00:00
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdVerbose) {
|
2020-03-31 08:37:10 +00:00
|
|
|
grpclog.SetLoggerV2(a.wlog)
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// conTimeout := a.cfg.GetDuration(cfgConTimeout)
|
|
|
|
// reqTimeout := a.cfg.GetDuration(cfgReqTimeout)
|
|
|
|
// tckTimeout := a.cfg.GetDuration(cfgRebalance)
|
2020-03-31 08:37:10 +00:00
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- setup FastHTTP server --
|
2020-03-31 08:37:10 +00:00
|
|
|
a.web.Name = "neofs-http-gate"
|
2020-12-02 09:09:46 +00:00
|
|
|
a.web.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
|
|
|
a.web.WriteBufferSize = a.cfg.GetInt(cfgWebWriteBufferSize)
|
|
|
|
a.web.ReadTimeout = a.cfg.GetDuration(cfgWebReadTimeout)
|
|
|
|
a.web.WriteTimeout = a.cfg.GetDuration(cfgWebWriteTimeout)
|
2020-03-31 08:37:10 +00:00
|
|
|
a.web.DisableHeaderNamesNormalizing = true
|
|
|
|
a.web.NoDefaultServerHeader = true
|
|
|
|
a.web.NoDefaultContentType = true
|
2021-02-12 12:24:52 +00:00
|
|
|
a.web.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize)
|
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- -- -- -- -- -- FIXME -- -- -- -- -- --
|
|
|
|
// Does not work with StreamRequestBody,
|
|
|
|
// some bugs with readMultipartForm
|
2021-02-12 15:48:52 +00:00
|
|
|
// https://github.com/valyala/fasthttp/issues/968
|
2021-02-13 16:17:01 +00:00
|
|
|
a.web.DisablePreParseMultipartForm = true
|
2021-02-12 15:48:52 +00:00
|
|
|
a.web.StreamRequestBody = a.cfg.GetBool(cfgWebStreamRequestBody)
|
2021-03-30 22:46:33 +00:00
|
|
|
// -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
2020-03-31 08:37:10 +00:00
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
var cl connectionList
|
2020-11-09 13:43:23 +00:00
|
|
|
for i := 0; ; i++ {
|
2020-12-02 09:09:46 +00:00
|
|
|
address := a.cfg.GetString(cfgPeers + "." + strconv.Itoa(i) + ".address")
|
|
|
|
weight := a.cfg.GetFloat64(cfgPeers + "." + strconv.Itoa(i) + ".weight")
|
2020-11-09 13:43:23 +00:00
|
|
|
if address == "" {
|
|
|
|
break
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
cl = append(cl, connection{address: address, weight: weight})
|
|
|
|
a.log.Info("add connection peer", zap.String("address", address), zap.Float64("weight", weight))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
sort.Sort(sort.Reverse(cl))
|
|
|
|
cred, err := neofs.NewCredentials(a.cfg.GetString(cmdNeoFSKey))
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Fatal("could not get credentials", zap.Error(err))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
a.plant, err = neofs.NewClientPlant(ctx, cl[0].address, cred)
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Fatal("failed to create neofs client")
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
a.getOperations.client, a.getOperations.sessionToken, err = a.plant.GetReusableArtifacts(ctx)
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Fatal("failed to get neofs client's reusable artifacts")
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2020-03-31 08:37:10 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Wait() {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("starting application")
|
2020-03-31 08:37:10 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-a.jobDone: // wait for job is stopped
|
|
|
|
<-a.webDone
|
|
|
|
case <-a.webDone: // wait for web-server is stopped
|
|
|
|
<-a.jobDone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Worker(ctx context.Context) {
|
|
|
|
close(a.jobDone)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) Serve(ctx context.Context) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("shutting down web server", zap.Error(a.web.Shutdown()))
|
2020-03-31 08:37:10 +00:00
|
|
|
close(a.webDone)
|
|
|
|
}()
|
2021-03-30 22:46:33 +00:00
|
|
|
// Configure router.
|
2020-03-31 08:37:10 +00:00
|
|
|
r := router.New()
|
|
|
|
r.RedirectTrailingSlash = true
|
2021-01-26 15:36:53 +00:00
|
|
|
r.POST("/upload/{cid}", a.upload)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /upload/{cid}")
|
2020-12-03 15:00:43 +00:00
|
|
|
r.GET("/get/{cid}/{oid}", a.byAddress)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /get/{cid}/{oid}")
|
2021-01-23 13:28:12 +00:00
|
|
|
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.byAttribute)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /get_by_attribute/{cid}/{attr_key}/{attr_val:*}")
|
2020-03-31 08:37:10 +00:00
|
|
|
// attaching /-/(ready,healthy)
|
2021-03-30 22:46:33 +00:00
|
|
|
// attachHealthy(r, a.pool.Status)
|
2020-03-31 08:37:10 +00:00
|
|
|
// enable metrics
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdMetrics) {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /metrics/")
|
2020-03-31 08:37:10 +00:00
|
|
|
attachMetrics(r, a.wlog)
|
|
|
|
}
|
|
|
|
// enable pprof
|
2020-12-02 09:09:46 +00:00
|
|
|
if a.cfg.GetBool(cmdPprof) {
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("added path /debug/pprof/")
|
2020-03-31 08:37:10 +00:00
|
|
|
attachProfiler(r)
|
|
|
|
}
|
2020-12-02 09:09:46 +00:00
|
|
|
bind := a.cfg.GetString(cfgListenAddress)
|
2021-03-30 22:46:33 +00:00
|
|
|
a.log.Info("running web server", zap.String("address", bind))
|
2020-03-31 08:37:10 +00:00
|
|
|
a.web.Handler = r.Handler
|
|
|
|
if err := a.web.ListenAndServe(bind); err != nil {
|
|
|
|
a.log.Fatal("could not start server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
|
|
|
|
type connection struct {
|
|
|
|
address string
|
|
|
|
weight float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type connectionList []connection
|
|
|
|
|
|
|
|
func (p connectionList) Len() int { return len(p) }
|
|
|
|
func (p connectionList) Less(i, j int) bool { return p[i].weight < p[j].weight }
|
|
|
|
func (p connectionList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|