-
Notifications
You must be signed in to change notification settings - Fork 203
/
main.go
151 lines (124 loc) · 3.43 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
"os"
"os/signal"
"sort"
"strconv"
"syscall"
"time"
"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/neblet"
"github.com/nebulasio/go-nebulas/util/logging"
"github.com/urfave/cli"
)
var (
version string
commit string
branch string
compileAt string
config string
)
func main() {
app := cli.NewApp()
app.Action = neb
app.Name = "neb"
app.Version = fmt.Sprintf("%s, branch %s, commit %s", version, branch, commit)
timestamp, _ := strconv.ParseInt(compileAt, 10, 64)
app.Compiled = time.Unix(timestamp, 0)
app.Usage = "the go-nebulas command line interface"
app.Copyright = "Copyright 2017-2018 The go-nebulas Authors"
app.Flags = append(app.Flags, ConfigFlag)
app.Flags = append(app.Flags, NetworkFlags...)
app.Flags = append(app.Flags, ChainFlags...)
app.Flags = append(app.Flags, RPCFlags...)
app.Flags = append(app.Flags, AppFlags...)
app.Flags = append(app.Flags, StatsFlags...)
sort.Sort(cli.FlagsByName(app.Flags))
app.Commands = []cli.Command{
initCommand,
genesisCommand,
accountCommand,
consoleCommand,
networkCommand,
versionCommand,
licenseCommand,
configCommand,
blockDumpCommand,
}
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}
func neb(ctx *cli.Context) error {
n, err := makeNeb(ctx)
if err != nil {
return err
}
logging.Init(n.Config().App.LogFile, n.Config().App.LogLevel, n.Config().App.LogAge)
core.SetCompatibilityOptions(n.Config().Chain.ChainId)
// enable crash report if open the switch and configure the url
if n.Config().App.EnableCrashReport && len(n.Config().App.CrashReportUrl) > 0 {
InitCrashReporter(n.Config().App)
}
select {
case <-runNeb(ctx, n):
return nil
}
}
func runNeb(ctx *cli.Context, n *neblet.Neblet) chan bool {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// start net pprof if config.App.Pprof.HttpListen configured
err := n.StartPprof(n.Config().App.Pprof.HttpListen)
if err != nil {
FatalF("start pprof failed:%s", err)
}
n.Setup()
n.Start()
quitCh := make(chan bool, 1)
go func() {
<-c
n.Stop()
quitCh <- true
return
}()
return quitCh
}
func makeNeb(ctx *cli.Context) (*neblet.Neblet, error) {
conf := neblet.LoadConfig(config)
conf.App.Version = version
// load config from cli args
networkConfig(ctx, conf.Network)
chainConfig(ctx, conf.Chain)
rpcConfig(ctx, conf.Rpc)
appConfig(ctx, conf.App)
statsConfig(ctx, conf.Stats)
n, err := neblet.New(conf)
if err != nil {
return nil, err
}
return n, nil
}
// FatalF fatal format err
func FatalF(format string, args ...interface{}) {
err := fmt.Sprintf(format, args...)
fmt.Println(err)
os.Exit(1)
}