server: add log-path and address configuration

- LogPath can be configured through config
- node,rpc and monitoring address can be configured thought command line
or config
This commit is contained in:
Vsevolod Brekelov 2019-11-05 15:22:07 +03:00
parent 1c08753915
commit 11ce73af28
15 changed files with 99 additions and 13 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/signal"
"path"
"github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core"
@ -107,11 +108,35 @@ func getConfigFromContext(ctx *cli.Context) (config.Config, error) {
return config.Load(configPath, net)
}
// handleLoggingParams enables debugging output is that's requested by the user.
func handleLoggingParams(ctx *cli.Context) {
// handleLoggingParams reads logging parameters.
// If user selected debug level -- function enables it.
// If logPath is configured -- function creates dir and file for logging.
func handleLoggingParams(ctx *cli.Context, cfg config.ApplicationConfiguration) error {
if ctx.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
if logPath := cfg.LogPath; logPath != "" {
if err := makeDir(logPath); err != nil {
return err
}
f, err := os.Create(logPath)
if err != nil {
return err
}
log.SetOutput(f)
}
return nil
}
func makeDir(filePath string) error {
fileName := filePath
dir := path.Dir(fileName)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create dir for logger: %v", err)
}
return nil
}
func getCountAndSkipFromContext(ctx *cli.Context) (uint32, uint32) {
@ -125,7 +150,9 @@ func dumpDB(ctx *cli.Context) error {
if err != nil {
return cli.NewExitError(err, 1)
}
handleLoggingParams(ctx)
if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
return cli.NewExitError(err, 1)
}
count, skip := getCountAndSkipFromContext(ctx)
var outStream = os.Stdout
@ -173,7 +200,9 @@ func restoreDB(ctx *cli.Context) error {
if err != nil {
return err
}
handleLoggingParams(ctx)
if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
return cli.NewExitError(err, 1)
}
count, skip := getCountAndSkipFromContext(ctx)
var inStream = os.Stdin
@ -234,7 +263,9 @@ func startServer(ctx *cli.Context) error {
if err != nil {
return err
}
handleLoggingParams(ctx)
if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
return err
}
grace, cancel := context.WithCancel(newGraceContext())
defer cancel()
@ -246,6 +277,7 @@ func startServer(ctx *cli.Context) error {
return err
}
configureAddresses(cfg.ApplicationConfiguration)
server := network.NewServer(serverConfig, chain)
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
errChan := make(chan error)
@ -285,6 +317,21 @@ Main:
return nil
}
// configureAddresses sets up addresses for RPC and Monitoring depending from the provided config.
// In case RPC or Monitoring Address provided each of them will use it.
// In case global Address (of the node) provided and RPC/Monitoring don't have configured addresses they will
// use global one. So Node and RPC and Monitoring will run on one address.
func configureAddresses(cfg config.ApplicationConfiguration) {
if cfg.Address != "" {
if cfg.RPC.Address == "" {
cfg.RPC.Address = cfg.Address
}
if cfg.Monitoring.Address == "" {
cfg.Monitoring.Address = cfg.Address
}
}
}
// initBlockChain initializes BlockChain with preselected DB.
func initBlockChain(cfg config.Config) (*core.Blockchain, error) {
store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration)

View file

@ -66,7 +66,9 @@ type (
// ApplicationConfiguration config specific to the node.
ApplicationConfiguration struct {
LogPath string `yaml:"LogPath"`
DBConfiguration storage.DBConfiguration `yaml:"DBConfiguration"`
Address string `yaml:"Address"`
NodePort uint16 `yaml:"NodePort"`
Relay bool `yaml:"Relay"`
DialTimeout time.Duration `yaml:"DialTimeout"`

View file

@ -31,6 +31,8 @@ ProtocolConfiguration:
VerifyTransactions: false
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -42,6 +44,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/mainnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 10333
Relay: true
DialTimeout: 3

View file

@ -19,6 +19,8 @@ ProtocolConfiguration:
VerifyTransactions: true
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -30,6 +32,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/privnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20337
Relay: true
DialTimeout: 3

View file

@ -16,6 +16,8 @@ ProtocolConfiguration:
VerifyTransactions: true
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -27,6 +29,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/privnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20334
Relay: true
DialTimeout: 3

View file

@ -16,6 +16,8 @@ ProtocolConfiguration:
VerifyTransactions: true
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -27,6 +29,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/privnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20336
Relay: true
DialTimeout: 3

View file

@ -16,6 +16,8 @@ ProtocolConfiguration:
VerifyTransactions: true
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -27,6 +29,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/privnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20335
Relay: true
DialTimeout: 3

View file

@ -22,6 +22,8 @@ ProtocolConfiguration:
VerifyTransactions: true
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -33,6 +35,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/privnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20332
Relay: true
DialTimeout: 3

View file

@ -31,6 +31,8 @@ ProtocolConfiguration:
VerifyTransactions: false
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "leveldb" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -42,6 +44,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/testnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20333
Relay: true
DialTimeout: 3

View file

@ -21,6 +21,8 @@ ProtocolConfiguration:
VerifyTransactions: true
ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration:
Type: "inmemory" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type.
@ -32,6 +34,8 @@ ApplicationConfiguration:
# DB: 0
# BoltDBOptions:
# FilePath: "./chains/unit_testnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20333
Relay: true
DialTimeout: 3

View file

@ -18,6 +18,7 @@ type Service struct {
// Additional information about Prometheus could be found here: https://prometheus.io/docs/guides/go-application.
type PrometheusConfig struct {
Enabled bool `yaml:"Enabled"`
Address string `yaml:"Address"`
Port string `yaml:"Port"`
}
@ -25,7 +26,7 @@ type PrometheusConfig struct {
func NewMetricsService(cfg PrometheusConfig) *Service {
return &Service{
&http.Server{
Addr: ":" + cfg.Port,
Addr: cfg.Address + ":" + cfg.Port,
Handler: promhttp.Handler(),
}, cfg,
}

View file

@ -104,7 +104,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer) *Server {
s.AttemptConnPeers = defaultAttemptConnPeers
}
s.transport = NewTCPTransport(s, fmt.Sprintf(":%d", config.ListenTCP))
s.transport = NewTCPTransport(s, fmt.Sprintf("%s:%d", config.Address, config.Port))
s.discovery = NewDefaultDiscovery(
s.DialTimeout,
s.transport,
@ -282,7 +282,7 @@ func (s *Server) startProtocol(p Peer) {
func (s *Server) sendVersion(p Peer) error {
payload := payload.NewVersion(
s.id,
s.ListenTCP,
s.Port,
s.UserAgent,
s.chain.BlockHeight(),
s.Relay,

View file

@ -27,8 +27,11 @@ type (
// The user agent of the server.
UserAgent string
// The listen address of the TCP server.
ListenTCP uint16
// Address. Example: "127.0.0.1".
Address string
// Port. Example: 20332.
Port uint16
// The network mode the server will operate on.
// ModePrivNet docker private network.
@ -62,7 +65,8 @@ func NewServerConfig(cfg config.Config) ServerConfig {
return ServerConfig{
UserAgent: cfg.GenerateUserAgent(),
ListenTCP: appConfig.NodePort,
Address: appConfig.Address,
Port: appConfig.NodePort,
Net: protoConfig.Magic,
Relay: appConfig.Relay,
Seeds: protoConfig.SeedList,

View file

@ -14,7 +14,7 @@ func TestSendVersion(t *testing.T) {
s = newTestServer()
p = newLocalPeer(t)
)
s.ListenTCP = 3000
s.Port = 3000
s.UserAgent = "/test/"
p.messageHandler = func(t *testing.T, msg *Message) {

View file

@ -179,7 +179,7 @@ Methods:
case "getversion":
getversionCalled.Inc()
results = result.Version{
Port: s.coreServer.ListenTCP,
Port: s.coreServer.Port,
Nonce: s.coreServer.ID(),
UserAgent: s.coreServer.UserAgent,
}