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
package main
import (
"encoding/json"
"example.com/m/conf"
"example.com/m/log"
"example.com/m/nm"
"example.com/m/utils"
"fmt"
"github.com/astaxie/beego"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"os"
)
var (
rewardAddr, dockerServer, externalIp string
)
func init() {
RootCmd.PersistentFlags().StringVarP(&rewardAddr, "reward", "r", "", "please enter a reward address")
RootCmd.PersistentFlags().StringVarP(&dockerServer, "docker_server", "d", "", "please enter docker server address")
RootCmd.PersistentFlags().StringVarP(&externalIp, "externalIp", "e", "", "please enter server external ip address")
cobra.OnInitialize(initConfig)
}
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "miner",
Short: "The miner command-line interface",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if rewardAddr == "" {
log.Error("Enter reward address is not nil")
return
}
isSetDockerServer := conf.GetConfig().SetDockerServerUrl(dockerServer)
if !isSetDockerServer {
log.Error("Enter right docker server address:", dockerServer)
return
}
log.Info("Enter docker server url:", dockerServer)
isSetReward := conf.GetConfig().SetRewardAddress(rewardAddr)
if !isSetReward {
log.Error("Please set right reward address")
return
}
conf.GetConfig().SetExternalIp(externalIp)
log.Info("Enter reward address:", rewardAddr)
log.InitLog(log.LogConfig{Path: "logs", Level: "debug", Save: 3})
go nm.StartMonitor()
beego.Run()
},
}
func initConfig() {
// 设置配置文件的名称(不包含扩展名)
viper.SetConfigName("config")
// 设置配置文件的类型
viper.SetConfigType("json")
// 设置配置文件所在的目录
viper.AddConfigPath(".")
viper.AutomaticEnv()
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config file:", err)
return
}
configFilePath := viper.ConfigFileUsed()
if configFilePath == "" {
// handle error
log.Error("config file path is empty")
panic("config file path is empty")
}
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
// handle error
log.Error("Read cfg file error:", err)
panic("Read cfg file error")
}
err = json.Unmarshal(data, conf.GetConfig())
if err != nil {
// handle error
log.Error("Json unmarshal cfg error:", err)
panic("Json unmarshal cfg error")
}
conf.GetConfig().HeartRespTimeMillis = conf.GetConfig().HeartRespTimeSecond * 60 * 60 * 1000
prvKey, err := utils.GetPrv()
if err != nil {
panic("get prv error or delete keystore after restart")
}
conf.GetConfig().SignPrivateKey = prvKey
ecdsaPub := prvKey.PublicKey
conf.GetConfig().SignPub = common.Bytes2Hex(crypto.FromECDSAPub(&ecdsaPub))
log.Info("PublicKey", conf.GetConfig().SignPub)
publicAddr := crypto.PubkeyToAddress(ecdsaPub)
log.Info("publicAddr:", publicAddr)
conf.GetConfig().SignPublicAddress = publicAddr
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
log.Error("root cmd execute failed", err)
os.Exit(-1)
}
}