-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain.go
159 lines (133 loc) · 3.23 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"runtime"
_ "net/http/pprof"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pojol/braid-go"
"github.com/pojol/braid-go/components"
"github.com/pojol/braid-go/components/discoverk8s"
"github.com/pojol/braid-go/module/meta"
"github.com/pojol/gobot/constant"
"github.com/pojol/gobot/factory"
"github.com/pojol/gobot/mock"
"github.com/pojol/gobot/server"
"github.com/redis/go-redis/v9"
)
var (
help bool
dbmode bool
cluster bool
scriptPath string
openHttpMock bool
openTcpMock bool
)
const (
// Version of gobot driver
Version = "v0.4.0"
banner = `
__ __
/\ \ /\ \__
__ ___\ \ \____ ___\ \ ,_\
/'_ '\ / __'\ \ '__'\ / __'\ \ \
/\ \L\ \/\ \L\ \ \ \L\ \/\ \L\ \ \ \_
\ \____ \ \____/\ \_,__/\ \____/\ \__\
\/___L\ \/___/ \/___/ \/___/ \/__/
/\____/
\_/__/ %s
`
)
func initFlag() {
flag.BoolVar(&help, "h", false, "this help")
flag.BoolVar(&cluster, "cluster", false, "open cluster mode")
flag.BoolVar(&dbmode, "no_database", false, "Run in local mode")
flag.BoolVar(&openHttpMock, "httpmock", false, "open http mock server")
flag.BoolVar(&openTcpMock, "tcpmock", false, "open tcp mock server")
flag.StringVar(&scriptPath, "script_path", "script/", "Path to bot script")
}
func main() {
defer func() {
if err := recover(); err != nil {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
fmt.Println("panic:", string(buf[:n]))
}
}()
initFlag()
flag.Parse()
if help {
flag.Usage()
return
}
_, err := factory.Create(
factory.WithNoDatabase(dbmode),
)
if err != nil {
panic(err)
}
fmt.Println("open cluster mode", cluster)
if cluster {
constant.SetClusterState(true)
redis_addr := os.Getenv("REDIS_ADDR")
b, _ := braid.NewService(
"bot",
os.Getenv("POD_NAME"),
&components.DefaultDirector{
Opts: &components.DirectorOpts{
RedisCliOpts: &redis.Options{
Addr: redis_addr,
},
DiscoverOpts: []discoverk8s.Option{
discoverk8s.WithNamespace("bot"),
discoverk8s.WithSelectorTag("bot"),
},
},
},
)
b.Init()
b.Run()
defer b.Close()
statechan, err := braid.Topic(meta.TopicElectionChangeState).Sub(context.TODO(), "election"+uuid.NewString())
if err != nil {
panic(err)
}
defer statechan.Close()
statechan.Arrived(func(msg *meta.Message) error {
smsg := meta.DecodeStateChangeMsg(msg)
constant.SetServerState(smsg.State)
return nil
})
}
fmt.Println("open http mock", openHttpMock)
if openHttpMock {
ms := mock.NewHttpServer()
go ms.Start(":6666")
defer ms.Close()
}
fmt.Println("open tcp mock", openTcpMock)
if openTcpMock {
tcpls := mock.StarTCPServer(":6667")
defer tcpls.Close()
}
go func() {
http.ListenAndServe(":6060", nil)
}()
// 查看有没有未完成的队列
factory.Global.CheckTaskHistory()
e := echo.New()
e.HideBanner = true
e.Use(middleware.CORS())
e.Use(middleware.Recover())
server.Route(e)
e.Start(":8888")
// Stop the service gracefully.
if err := e.Shutdown(context.TODO()); err != nil {
panic(err)
}
}