-
Notifications
You must be signed in to change notification settings - Fork 319
/
main.go
114 lines (98 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
// Usage:
// make build
// ./bin/server -config-file=./config.yaml
//
package main
import (
"context"
"flag"
"fmt"
glog "log"
"os"
"os/signal"
"syscall"
_ "go.uber.org/automaxprocs"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/probe"
"github.com/iotexproject/iotex-core/server/itx"
)
func init() {
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr,
"usage: server -config-path=[string]\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
}
func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
stopped := make(chan struct{})
livenessCtx, livenessCancel := context.WithCancel(context.Background())
genesisCfg, err := genesis.New()
if err != nil {
glog.Fatalln("Failed to new genesis config.", zap.Error(err))
}
cfg, err := config.New()
if err != nil {
glog.Fatalln("Failed to new config.", zap.Error(err))
}
initLogger(cfg)
cfg.Genesis = genesisCfg
cfgToLog := cfg
cfgToLog.Chain.ProducerPrivKey = ""
log.S().Infof("Config in use: %+v", cfgToLog)
// liveness start
probeSvr := probe.New(cfg.System.HTTPStatsPort)
if err := probeSvr.Start(ctx); err != nil {
log.L().Fatal("Failed to start probe server.", zap.Error(err))
}
go func() {
<-stop
// start stopping
cancel()
<-stopped
// liveness end
if err := probeSvr.Stop(livenessCtx); err != nil {
log.L().Error("Error when stopping probe server.", zap.Error(err))
}
livenessCancel()
}()
// create and start the node
svr, err := itx.NewServer(cfg)
if err != nil {
log.L().Fatal("Failed to create server.", zap.Error(err))
}
cfgsub, err := config.NewSub()
if err != nil {
log.L().Fatal("Failed to new sub chain config.", zap.Error(err))
}
if cfgsub.Chain.ID != 0 {
if err := svr.NewSubChainService(cfgsub); err != nil {
log.L().Fatal("Failed to new sub chain.", zap.Error(err))
}
}
itx.StartServer(ctx, svr, probeSvr, cfg)
close(stopped)
<-livenessCtx.Done()
}
func initLogger(cfg config.Config) {
addr := cfg.ProducerAddress()
if err := log.InitLoggers(cfg.Log, cfg.SubLogs, zap.Fields(
zap.String("ioAddr", addr.String()),
zap.String("networkAddr", fmt.Sprintf("%s:%d", cfg.Network.Host, cfg.Network.Port)),
)); err != nil {
glog.Println("Cannot config global logger, use default one: ", err)
}
}