-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (133 loc) · 4.14 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
package main
import (
"errors"
"flag"
"fmt"
"github.com/devfeel/rockman/config"
"github.com/devfeel/rockman/logger"
"github.com/devfeel/rockman/metrics/prometheus"
"github.com/devfeel/rockman/node"
"github.com/devfeel/rockman/rpc"
"github.com/devfeel/rockman/util/exception"
"github.com/devfeel/rockman/webui"
"os"
"time"
)
var CurNode *node.Node
var CurRpcServer *rpc.RpcServer
var CurWebServer *webui.WebServer
const (
ProjectName = "rockman"
cmdNodeType = "nodetype"
cmdOuterHost = "outerhost"
cmdOuterPort = "outerport"
cmdCluster = "cluster"
cmdEnableTls = "enabletls"
version = "2020.0606 For Birthday on 2020"
confName = "app.conf"
)
func main() {
defer func() {
if err := recover(); err != nil {
exMsg := exception.CatchError(ProjectName+":main", err)
logger.Default().Error(errors.New(exMsg), "")
os.Stdout.Write([]byte(exMsg))
}
}()
startTime := time.Now()
printLogo()
var err error
// load config file
profile, err := config.LoadConfig(confName)
if err != nil {
logger.Default().Error(err, "load config error")
return
}
parseFlag(profile)
// start log service
logger.StartLogService("config")
shutdownChan := make(chan string)
//start worker node
CurNode, err = node.NewNode(profile, shutdownChan)
if err != nil {
logger.Default().Error(err, "new node error")
return
}
//start rpc server
CurRpcServer = rpc.NewRpcServer(profile, CurNode)
//start web server
if profile.Node.IsMaster {
CurWebServer = webui.NewWebServer(profile.Logger.LogPath, CurNode)
}
go func() {
err := CurRpcServer.Listen()
if err != nil {
logger.Default().Error(err, "RpcServer start error")
panic(errors.New("RpcServer start error: " + err.Error()))
}
}()
if profile.Node.IsMaster {
go func() {
err := CurWebServer.ListenAndServe(profile.WebUI.HttpHost + ":" + profile.WebUI.HttpPort)
if err != nil {
logger.Default().Error(err, "WebUI start error")
panic(errors.New("WebUI start error: " + err.Error()))
}
}()
}
time.Sleep(time.Second)
if profile.Prometheus.IsRun {
go func() {
err := prometheus.StartMetricsWeb(profile.Prometheus.HttpHost + ":" + profile.Prometheus.HttpPort)
if err != nil {
logger.Default().Error(err, "Prometheus metrics start error")
panic(errors.New("Prometheus metrics start error: " + err.Error()))
}
}()
}
time.Sleep(time.Second)
//start node
err = CurNode.Start()
if err != nil {
logger.Default().Error(err, "Node start error")
return
}
useTime := time.Now().Sub(startTime)
logger.Default().Debug("Node start success in " + fmt.Sprint(int64(useTime/time.Second)) + "s, service running...")
<-shutdownChan
logger.Default().Debug("Node Shutdown.")
logger.Default().Debug("Node Close.")
}
func parseFlag(profile *config.Profile) {
var nodeType, outerHost, outerPort, cluster string
var enableTls bool
flag.StringVar(&nodeType, cmdNodeType, "", "node type, full or master or worker")
flag.StringVar(&outerHost, cmdOuterHost, "", "node outer host")
flag.StringVar(&outerPort, cmdOuterPort, "", "node outer port")
flag.StringVar(&cluster, cmdCluster, "", "node cluster id")
flag.BoolVar(&enableTls, cmdEnableTls, false, "enable tls for rpc")
flag.Parse()
if nodeType == "master" {
profile.Node.IsWorker = false
}
if nodeType == "worker" {
profile.Node.IsMaster = false
}
profile.Rpc.OuterHost = outerHost
profile.Rpc.OuterPort = outerPort
if cluster != "" {
profile.Cluster.ClusterId = cluster
}
if enableTls {
profile.Rpc.EnableTls = enableTls
}
}
func printLogo() {
fmt.Println(".______ ______ ______ __ ___ .___ ___. ___ .__ __. ")
fmt.Println("| _ \\ / __ \\ / || |/ / | \\/ | / \\ | \\ | | ")
fmt.Println("| |_) | | | | | | ,----'| ' / | \\ / | / ^ \\ | \\| | ")
fmt.Println("| / | | | | | | | < | |\\/| | / /_\\ \\ | . ` | ")
fmt.Println("| |\\ \\--.| `--' | | `----.| . \\ | | | | / _____ \\ | |\\ | ")
fmt.Println("| _| `.___| \\______/ \\______||__|\\__\\ |__| |__| /__/ \\__\\ |__| \\__| ")
fmt.Printf("%c[1m%s%c[0m\n", 0x1B, " Version: Beta."+version, 0x1B)
}