[#493] ir: Replace creation of HTTP servers into a separate function

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-12 10:35:18 +03:00 committed by Alex Vanin
parent 8d17dab86e
commit c340d77b74

View file

@ -14,6 +14,7 @@ import (
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http" httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
"github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -59,33 +60,7 @@ func main() {
ctx := grace.NewGracefulContext(log) ctx := grace.NewGracefulContext(log)
intErr := make(chan error) // internal inner ring errors intErr := make(chan error) // internal inner ring errors
var httpServers []*httputil.Server httpServers := initHTTPServers(cfg)
for _, item := range []struct {
cfgPrefix string
handler func() http.Handler
}{
{"profiler", httputil.Handler},
{"metrics", promhttp.Handler},
} {
addr := cfg.GetString(item.cfgPrefix + ".address")
if addr == "" {
continue
}
var prm httputil.Prm
prm.Address = addr
prm.Handler = item.handler()
httpServers = append(httpServers,
httputil.New(prm,
httputil.WithShutdownTimeout(
cfg.GetDuration(item.cfgPrefix+".shutdown_timeout"),
),
),
)
}
innerRing, err := innerring.New(ctx, log, cfg) innerRing, err := innerring.New(ctx, log, cfg)
if err != nil { if err != nil {
@ -153,3 +128,35 @@ func parsePublicKeysFromString(argument string) (keys.PublicKeys, error) {
return innerring.ParsePublicKeysFromStrings(publicKeysString) return innerring.ParsePublicKeysFromStrings(publicKeysString)
} }
func initHTTPServers(cfg *viper.Viper) []*httputil.Server {
var httpServers []*httputil.Server
for _, item := range []struct {
cfgPrefix string
handler func() http.Handler
}{
{"profiler", httputil.Handler},
{"metrics", promhttp.Handler},
} {
addr := cfg.GetString(item.cfgPrefix + ".address")
if addr == "" {
continue
}
var prm httputil.Prm
prm.Address = addr
prm.Handler = item.handler()
httpServers = append(httpServers,
httputil.New(prm,
httputil.WithShutdownTimeout(
cfg.GetDuration(item.cfgPrefix+".shutdown_timeout"),
),
),
)
}
return httpServers
}