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" "fmt"
"os" "os"
"os/signal" "os/signal"
"path"
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
@ -107,11 +108,35 @@ func getConfigFromContext(ctx *cli.Context) (config.Config, error) {
return config.Load(configPath, net) return config.Load(configPath, net)
} }
// handleLoggingParams enables debugging output is that's requested by the user. // handleLoggingParams reads logging parameters.
func handleLoggingParams(ctx *cli.Context) { // 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") { if ctx.Bool("debug") {
log.SetLevel(log.DebugLevel) 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) { func getCountAndSkipFromContext(ctx *cli.Context) (uint32, uint32) {
@ -125,7 +150,9 @@ func dumpDB(ctx *cli.Context) error {
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
handleLoggingParams(ctx) if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
return cli.NewExitError(err, 1)
}
count, skip := getCountAndSkipFromContext(ctx) count, skip := getCountAndSkipFromContext(ctx)
var outStream = os.Stdout var outStream = os.Stdout
@ -173,7 +200,9 @@ func restoreDB(ctx *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
handleLoggingParams(ctx) if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
return cli.NewExitError(err, 1)
}
count, skip := getCountAndSkipFromContext(ctx) count, skip := getCountAndSkipFromContext(ctx)
var inStream = os.Stdin var inStream = os.Stdin
@ -234,7 +263,9 @@ func startServer(ctx *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
handleLoggingParams(ctx) if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
return err
}
grace, cancel := context.WithCancel(newGraceContext()) grace, cancel := context.WithCancel(newGraceContext())
defer cancel() defer cancel()
@ -246,6 +277,7 @@ func startServer(ctx *cli.Context) error {
return err return err
} }
configureAddresses(cfg.ApplicationConfiguration)
server := network.NewServer(serverConfig, chain) server := network.NewServer(serverConfig, chain)
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server) rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
errChan := make(chan error) errChan := make(chan error)
@ -285,6 +317,21 @@ Main:
return nil 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. // initBlockChain initializes BlockChain with preselected DB.
func initBlockChain(cfg config.Config) (*core.Blockchain, error) { func initBlockChain(cfg config.Config) (*core.Blockchain, error) {
store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration) store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -21,6 +21,8 @@ ProtocolConfiguration:
VerifyTransactions: true VerifyTransactions: true
ApplicationConfiguration: ApplicationConfiguration:
# LogPath could be set up in case you need stdout logs to some proper file.
# LogPath: "./log/neogo.log"
DBConfiguration: DBConfiguration:
Type: "inmemory" #other options: 'inmemory','redis','boltdb'. Type: "inmemory" #other options: 'inmemory','redis','boltdb'.
# DB type options. Uncomment those you need in case you want to switch DB type. # DB type options. Uncomment those you need in case you want to switch DB type.
@ -32,6 +34,8 @@ ApplicationConfiguration:
# DB: 0 # DB: 0
# BoltDBOptions: # BoltDBOptions:
# FilePath: "./chains/unit_testnet.bolt" # FilePath: "./chains/unit_testnet.bolt"
# Uncomment in order to set up custom address for node.
# Address: 127.0.0.1
NodePort: 20333 NodePort: 20333
Relay: true Relay: true
DialTimeout: 3 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. // Additional information about Prometheus could be found here: https://prometheus.io/docs/guides/go-application.
type PrometheusConfig struct { type PrometheusConfig struct {
Enabled bool `yaml:"Enabled"` Enabled bool `yaml:"Enabled"`
Address string `yaml:"Address"`
Port string `yaml:"Port"` Port string `yaml:"Port"`
} }
@ -25,7 +26,7 @@ type PrometheusConfig struct {
func NewMetricsService(cfg PrometheusConfig) *Service { func NewMetricsService(cfg PrometheusConfig) *Service {
return &Service{ return &Service{
&http.Server{ &http.Server{
Addr: ":" + cfg.Port, Addr: cfg.Address + ":" + cfg.Port,
Handler: promhttp.Handler(), Handler: promhttp.Handler(),
}, cfg, }, cfg,
} }

View file

@ -104,7 +104,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer) *Server {
s.AttemptConnPeers = defaultAttemptConnPeers 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.discovery = NewDefaultDiscovery(
s.DialTimeout, s.DialTimeout,
s.transport, s.transport,
@ -282,7 +282,7 @@ func (s *Server) startProtocol(p Peer) {
func (s *Server) sendVersion(p Peer) error { func (s *Server) sendVersion(p Peer) error {
payload := payload.NewVersion( payload := payload.NewVersion(
s.id, s.id,
s.ListenTCP, s.Port,
s.UserAgent, s.UserAgent,
s.chain.BlockHeight(), s.chain.BlockHeight(),
s.Relay, s.Relay,

View file

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

View file

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

View file

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