-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
170 lines (162 loc) · 4.58 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"
"github.com/ontio/mercury/cmd"
"github.com/ontio/mercury/cmd/did"
http_cmd "github.com/ontio/mercury/cmd/httpclient"
"github.com/ontio/mercury/common/config"
"github.com/ontio/mercury/common/log"
"github.com/ontio/mercury/common/packager/ecdsa"
"github.com/ontio/mercury/service"
"github.com/ontio/mercury/service/common"
store "github.com/ontio/mercury/store/leveldb"
"github.com/ontio/mercury/utils"
"github.com/ontio/mercury/vdri/ontdid"
sdk "github.com/ontio/ontology-go-sdk"
"github.com/urfave/cli"
)
func setupAPP() *cli.App {
app := cli.NewApp()
app.Usage = "agent mercury"
app.Action = startAgent
app.Flags = []cli.Flag{
cmd.LogLevelFlag,
cmd.LogDirFlag,
cmd.HttpIpFlag,
cmd.HttpPortFlag,
cmd.RestUrlFlag,
cmd.RpcUrlFlag,
cmd.HttpsPortFlag,
cmd.SelfDIDFlag,
cmd.EnableHttpsFlag,
cmd.EnablePackageFlag,
}
app.Commands = []cli.Command{
did.DidCommand,
http_cmd.HttpClientCmd,
}
app.Before = func(context *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
return nil
}
return app
}
func main() {
if err := setupAPP().Run(os.Args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func startAgent(ctx *cli.Context) {
initLog(ctx)
ontSdk := sdk.NewOntologySdk()
ontSdk.NewRpcClient().SetAddress(ctx.String(cmd.GetFlagName(cmd.RpcUrlFlag)))
account, err := utils.OpenAccount(cmd.DEFAULT_WALLET_PATH, ontSdk)
if err != nil {
panic(err)
}
var port string
if ctx.Bool(cmd.GetFlagName(cmd.EnableHttpsFlag)) {
port = ctx.String(cmd.GetFlagName(cmd.HttpsPortFlag))
} else {
port = ctx.String(cmd.GetFlagName(cmd.HttpPortFlag))
}
if ctx.Bool(cmd.GetFlagName(cmd.EnablePackageFlag)) {
common.EnablePackage = true
}
selfDid := ctx.String(cmd.GetFlagName(cmd.SelfDIDFlag))
ip := ctx.String(cmd.GetFlagName(cmd.HttpIpFlag))
prov := store.NewProvider(cmd.DEFAULT_STORE_DIR)
db, err := prov.OpenStore(cmd.DEFAULT_STORE_DIR)
if err != nil {
panic(err)
}
cfg := &config.Cfg{
Port: port,
Ip: ip,
SelfDID: selfDid,
}
ontVdri := ontdid.NewOntVDRI(ontSdk, account, selfDid)
msgSvr := common.NewMessageService(ontVdri, ontSdk, account, ctx.Bool(cmd.GetFlagName(cmd.EnablePackageFlag)), cfg)
r := service.NewApiRouter(ecdsa.New(ontSdk, account), db, msgSvr, ontVdri)
log.Infof("start agent mercury account:%s,port:%s", account.Address.ToBase58(), cfg.Port)
startPort := ip + ":" + port
if ctx.Bool(cmd.GetFlagName(cmd.EnableHttpsFlag)) {
err = r.RunTLS(startPort, cmd.DEFAULT_CERT_PATH, cmd.DEFAULT_KEY_PATH)
if err != nil {
panic(err)
}
} else {
err = r.Run(startPort)
if err != nil {
panic(err)
}
}
go CheckLogFileSize(ctx)
signalHandle()
}
func initLog(ctx *cli.Context) {
logLevel := ctx.GlobalInt(cmd.GetFlagName(cmd.LogLevelFlag))
disableLogFile := ctx.GlobalBool(cmd.GetFlagName(cmd.DisableLogFileFlag))
if disableLogFile {
log.InitLog(logLevel, log.Stdout)
} else {
logFileDir := ctx.String(cmd.GetFlagName(cmd.LogDirFlag))
logFileDir = filepath.Join(logFileDir, "") + string(os.PathSeparator)
log.InitLog(logLevel, logFileDir, log.Stdout)
}
}
func CheckLogFileSize(ctx *cli.Context) {
ticker := time.NewTicker(cmd.DEFAULT_CHECK_LOG * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
isNeedNewFile := log.CheckIfNeedNewFile()
if isNeedNewFile {
log.ClosePrintLog()
log.InitLog(ctx.GlobalInt(cmd.GetFlagName(cmd.LogLevelFlag)), log.PATH, log.Stdout)
}
}
}
}
func signalHandle() {
var (
ch = make(chan os.Signal, 1)
)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
si := <-ch
switch si {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
fmt.Println("get a signal: stop the agent mercury process", si.String())
return
case syscall.SIGHUP:
default:
return
}
}
}