config: unify BasicService config within existing services

There are no changes visible from the user side (at least for those
users who doesn't put Prometheus's or pprof's port in quotes), just
internal refactoring. From now and on, BasicService configuration is
used by RPC server config, TLS for RPC server, pprof and Prometheus.
This commit is contained in:
Anna Shaleva 2022-11-24 16:03:08 +03:00
parent d6b99a9a27
commit 6b4dd5703e
9 changed files with 63 additions and 16 deletions

View file

@ -330,8 +330,10 @@ func TestConfigureAddresses(t *testing.T) {
cfg := &config.ApplicationConfiguration{ cfg := &config.ApplicationConfiguration{
Address: defaultAddress, Address: defaultAddress,
RPC: config.RPC{ RPC: config.RPC{
BasicService: config.BasicService{
Address: customAddress, Address: customAddress,
}, },
},
} }
configureAddresses(cfg) configureAddresses(cfg)
require.Equal(t, cfg.RPC.Address, customAddress) require.Equal(t, cfg.RPC.Address, customAddress)

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
) )
// MarshalUnmarshalJSON checks if the expected stays the same after // MarshalUnmarshalJSON checks if the expected stays the same after
@ -18,6 +19,15 @@ func MarshalUnmarshalJSON(t *testing.T, expected, actual interface{}) {
require.Equal(t, expected, actual) require.Equal(t, expected, actual)
} }
// MarshalUnmarshalYAML checks if the expected stays the same after
// marshal/unmarshal via YAML.
func MarshalUnmarshalYAML(t *testing.T, expected, actual interface{}) {
data, err := yaml.Marshal(expected)
require.NoError(t, err)
require.NoError(t, yaml.Unmarshal(data, actual))
require.Equal(t, expected, actual)
}
// EncodeDecodeBinary checks if the expected stays the same after // EncodeDecodeBinary checks if the expected stays the same after
// serializing/deserializing via io.Serializable methods. // serializing/deserializing via io.Serializable methods.
func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) { func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) {

View file

@ -20,3 +20,12 @@ func TestApplicationConfigurationEquals(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.False(t, cfg1.ApplicationConfiguration.EqualsButServices(&cfg2.ApplicationConfiguration)) require.False(t, cfg1.ApplicationConfiguration.EqualsButServices(&cfg2.ApplicationConfiguration))
} }
// TestApplicationConfiguration_UnmarshalRPCBasicService is aimed to check that BasicService
// config of RPC service can be properly unmarshalled.
func TestApplicationConfiguration_UnmarshalRPCBasicService(t *testing.T) {
cfg, err := LoadFile(filepath.Join("..", "..", "config", "protocol.mainnet.yml"))
require.NoError(t, err)
require.True(t, cfg.ApplicationConfiguration.RPC.Enabled)
require.Equal(t, uint16(10332), cfg.ApplicationConfiguration.RPC.Port)
}

View file

@ -1,8 +1,19 @@
package config package config
// BasicService is used for simple services like Pprof or Prometheus monitoring. import (
"net"
"strconv"
)
// BasicService is used as a simple base for node services like Pprof, RPC or
// Prometheus monitoring.
type BasicService struct { type BasicService struct {
Enabled bool `yaml:"Enabled"` Enabled bool `yaml:"Enabled"`
Address string `yaml:"Address"` Address string `yaml:"Address"`
Port string `yaml:"Port"` Port uint16 `yaml:"Port"`
}
// FormatAddress returns the full service's address in the form of "address:port".
func (s BasicService) FormatAddress() string {
return net.JoinHostPort(s.Address, strconv.FormatUint(uint64(s.Port), 10))
} }

View file

@ -0,0 +1,19 @@
package config
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestBasicService_FormatAddress(t *testing.T) {
for expected, tc := range map[string]BasicService{
"localhost:10332": {Address: "localhost", Port: 10332},
"127.0.0.1:0": {Address: "127.0.0.1"},
":0": {},
} {
t.Run(expected, func(t *testing.T) {
require.Equal(t, expected, tc.FormatAddress())
})
}
}

View file

@ -7,8 +7,7 @@ import (
type ( type (
// RPC is an RPC service configuration information. // RPC is an RPC service configuration information.
RPC struct { RPC struct {
Address string `yaml:"Address"` BasicService `yaml:",inline"`
Enabled bool `yaml:"Enabled"`
EnableCORSWorkaround bool `yaml:"EnableCORSWorkaround"` EnableCORSWorkaround bool `yaml:"EnableCORSWorkaround"`
// MaxGasInvoke is the maximum amount of GAS which // MaxGasInvoke is the maximum amount of GAS which
// can be spent during an RPC call. // can be spent during an RPC call.
@ -17,7 +16,6 @@ type (
MaxFindResultItems int `yaml:"MaxFindResultItems"` MaxFindResultItems int `yaml:"MaxFindResultItems"`
MaxNEP11Tokens int `yaml:"MaxNEP11Tokens"` MaxNEP11Tokens int `yaml:"MaxNEP11Tokens"`
MaxWebSocketClients int `yaml:"MaxWebSocketClients"` MaxWebSocketClients int `yaml:"MaxWebSocketClients"`
Port uint16 `yaml:"Port"`
SessionEnabled bool `yaml:"SessionEnabled"` SessionEnabled bool `yaml:"SessionEnabled"`
SessionExpirationTime int `yaml:"SessionExpirationTime"` SessionExpirationTime int `yaml:"SessionExpirationTime"`
SessionBackedByMPT bool `yaml:"SessionBackedByMPT"` SessionBackedByMPT bool `yaml:"SessionBackedByMPT"`
@ -28,10 +26,8 @@ type (
// TLS describes SSL/TLS configuration. // TLS describes SSL/TLS configuration.
TLS struct { TLS struct {
Address string `yaml:"Address"` BasicService `yaml:",inline"`
CertFile string `yaml:"CertFile"` CertFile string `yaml:"CertFile"`
Enabled bool `yaml:"Enabled"`
Port uint16 `yaml:"Port"`
KeyFile string `yaml:"KeyFile"` KeyFile string `yaml:"KeyFile"`
} }
) )

View file

@ -26,7 +26,7 @@ func NewPprofService(cfg config.BasicService, log *zap.Logger) *Service {
return &Service{ return &Service{
Server: &http.Server{ Server: &http.Server{
Addr: cfg.Address + ":" + cfg.Port, Addr: cfg.FormatAddress(),
Handler: handler, Handler: handler,
}, },
config: cfg, config: cfg,

View file

@ -19,7 +19,7 @@ func NewPrometheusService(cfg config.BasicService, log *zap.Logger) *Service {
return &Service{ return &Service{
Server: &http.Server{ Server: &http.Server{
Addr: cfg.Address + ":" + cfg.Port, Addr: cfg.FormatAddress(),
Handler: promhttp.Handler(), Handler: promhttp.Handler(),
}, },
config: cfg, config: cfg,

View file

@ -255,13 +255,13 @@ var invalidBlockHeightError = func(index int, height int) *neorpc.Error {
func New(chain Ledger, conf config.RPC, coreServer *network.Server, func New(chain Ledger, conf config.RPC, coreServer *network.Server,
orc OracleHandler, log *zap.Logger, errChan chan error) Server { orc OracleHandler, log *zap.Logger, errChan chan error) Server {
httpServer := &http.Server{ httpServer := &http.Server{
Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10), Addr: conf.FormatAddress(),
} }
var tlsServer *http.Server var tlsServer *http.Server
if cfg := conf.TLSConfig; cfg.Enabled { if cfg := conf.TLSConfig; cfg.Enabled {
tlsServer = &http.Server{ tlsServer = &http.Server{
Addr: net.JoinHostPort(cfg.Address, strconv.FormatUint(uint64(cfg.Port), 10)), Addr: cfg.FormatAddress(),
} }
} }