-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
97 lines (83 loc) · 2.33 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
package main
import (
"github.com/phachon/wmqx/app"
"github.com/phachon/wmqx/container"
"github.com/phachon/wmqx/message"
"github.com/valyala/fasthttp"
"os"
)
// WMQX RabbitMQ Callback
func main() {
initQMessage()
initRabbitMQPools()
startConsumerWorker()
initExchange()
startApiServer()
startPublishServer()
}
// init Ctx QMessage
func initQMessage() {
recordType := app.Conf.GetString("message.record_type")
filename := app.Conf.GetString("message.filename")
jsonBeautify := app.Conf.GetBool("message.jsonBeautify")
fileConfig := &message.RecordFileConfig{
Filename: filename,
JsonBeautify: jsonBeautify,
}
qm, err := message.NewQMessage(recordType, message.NewRecordConfigFile(fileConfig))
if err != nil {
app.Log.Errorf("Init QMessage Error: %s", err.Error())
os.Exit(1)
}
container.Ctx.QMessage = qm
app.Log.Infof("Init QMessage %s success!", recordType)
}
// init Ctx RabbitMq pools and check rabbitMQ conn
func initRabbitMQPools() {
poolNumber := app.Conf.GetInt("rabbitmq.poolNumber")
container.Ctx.SetRabbitMQPools(poolNumber)
mq, err := container.Ctx.RabbitMQPools.GetMQ()
defer container.Ctx.RabbitMQPools.Recover(mq)
if err != nil {
app.Log.Errorf("Init Rabbitmq pools falied: %s", err.Error())
os.Exit(1)
}
app.Log.Info("Init Rabbitmq pools success!")
}
// start consumer worker
func startConsumerWorker() {
container.Worker.Consumer()
app.Log.Info("Rabbitmq consumer worker start!")
}
// init RabbitMq exchange
func initExchange() {
container.Ctx.InitExchanges()
app.Log.Info("Init Rabbitmq exchange success!")
}
// start api server
func startApiServer() {
go func() {
defer func() {
e := recover()
if e != nil {
app.Log.Errorf("strat api server crash: %v", e)
}
}()
apiListen := app.Conf.GetString("listen.api")
app.Log.Info("Api server start listen: " + apiListen + "!")
err := fasthttp.ListenAndServe(apiListen, NewRouter().Api().Handler)
if err != nil {
app.Log.Errorf("Api server listen failed: %s", err.Error())
}
}()
}
// start publish server
func startPublishServer() {
publishListen := app.Conf.GetString("listen.publish")
app.Log.Info("Publish Server start listen: " + publishListen + "!")
err := fasthttp.ListenAndServe(publishListen, NewRouter().Publish().Handler)
if err != nil {
app.Log.Errorf("Publish Server listen failed: %s", err.Error())
os.Exit(1)
}
}