Skip to content

Commit 2678184

Browse files
optimize conf
1 parent f6c4b55 commit 2678184

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1191
-1173
lines changed

admin/checkpoint/bot.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
"sync"
1010
"time"
11-
11+
1212
"github.com/yincongcyincong/MuseBot/admin/conf"
1313
"github.com/yincongcyincong/MuseBot/admin/db"
1414
"github.com/yincongcyincong/MuseBot/admin/utils"
@@ -32,17 +32,17 @@ type BotStatus struct {
3232
}
3333

3434
func InitStatusCheck() {
35-
if *conf.RegisterConfInfo.Type != "" {
35+
if conf.RegisterConfInfo.Type != "" {
3636
go func() {
3737
InitRegister()
3838
}()
39-
39+
4040
} else {
4141
go func() {
4242
ManualCheckPoint()
4343
}()
4444
}
45-
45+
4646
}
4747

4848
func ManualCheckPoint() {
@@ -52,9 +52,9 @@ func ManualCheckPoint() {
5252
}
5353
}()
5454
ScheduleBotChecks()
55-
ticker := time.NewTicker(time.Duration(*conf.BaseConfInfo.CheckBotSec) * time.Second)
55+
ticker := time.NewTicker(time.Duration(conf.BaseConfInfo.CheckBotSec) * time.Second)
5656
defer ticker.Stop()
57-
57+
5858
for {
5959
select {
6060
case <-ticker.C:
@@ -72,12 +72,12 @@ func checkBotStatus(bot *db.Bot) string {
7272
return "offline" // 请求失败
7373
}
7474
defer resp.Body.Close()
75-
75+
7676
if resp.StatusCode != http.StatusOK {
7777
logger.Warn("checkpoint request fail", "resp", resp, "address", bot.Address)
7878
return OfflineStatus
7979
}
80-
80+
8181
return OnlineStatus
8282
}
8383

@@ -87,16 +87,16 @@ func ScheduleBotChecks() {
8787
logger.Error("ScheduleBotChecks panic", "err", err, "stack", string(debug.Stack()))
8888
}
8989
}()
90-
90+
9191
bots, _, err := db.ListBots(0, 10000, "")
9292
if err != nil {
9393
logger.Error("ScheduleBotChecks list bots fail", "err", err)
9494
return
9595
}
96-
97-
batchCount := *conf.BaseConfInfo.CheckBotSec
96+
97+
batchCount := conf.BaseConfInfo.CheckBotSec
9898
batchSize := (len(bots) + batchCount - 1) / batchCount
99-
99+
100100
var wg sync.WaitGroup
101101
for i := 0; i < batchCount; i++ {
102102
start := i * batchSize
@@ -107,10 +107,10 @@ func ScheduleBotChecks() {
107107
if start >= len(bots) {
108108
break
109109
}
110-
110+
111111
batch := bots[start:end]
112112
batchIndex := i
113-
113+
114114
wg.Add(1)
115115
go func(batch []*db.Bot, batchIndex int) {
116116
defer func() {
@@ -119,10 +119,10 @@ func ScheduleBotChecks() {
119119
}
120120
wg.Done()
121121
}()
122-
122+
123123
timer := time.NewTimer(time.Duration(batchIndex) * time.Second)
124124
<-timer.C
125-
125+
126126
for _, b := range batch {
127127
status := checkBotStatus(b)
128128
BotMap.Store(b.ID, &BotStatus{
@@ -135,12 +135,12 @@ func ScheduleBotChecks() {
135135
}
136136
}(batch, batchIndex)
137137
}
138-
138+
139139
wg.Wait()
140140
}
141141

142142
func InitRegister() {
143-
switch *conf.RegisterConfInfo.Type {
143+
switch conf.RegisterConfInfo.Type {
144144
case "etcd":
145145
InitEtcdRegister()
146146
}
@@ -150,34 +150,34 @@ func InitEtcdRegister() {
150150
if len(conf.RegisterConfInfo.EtcdURLs) == 0 {
151151
return
152152
}
153-
153+
154154
cli, err := clientv3.New(clientv3.Config{
155155
Endpoints: conf.RegisterConfInfo.EtcdURLs,
156156
DialTimeout: 5 * time.Second,
157-
Username: *conf.RegisterConfInfo.EtcdUsername,
158-
Password: *conf.RegisterConfInfo.EtcdPassword,
157+
Username: conf.RegisterConfInfo.EtcdUsername,
158+
Password: conf.RegisterConfInfo.EtcdPassword,
159159
})
160160
if err != nil {
161161
logger.Error("register init failed: ", err)
162162
return
163163
}
164164
defer cli.Close()
165-
165+
166166
ctx := context.Background()
167167
prefix := "/services/musebot/"
168-
168+
169169
resp, err := cli.Get(ctx, prefix, clientv3.WithPrefix())
170170
if err != nil {
171171
logger.Error("register get failed: ", err)
172172
return
173173
}
174-
174+
175175
for _, kv := range resp.Kvs {
176176
parts := strings.Split(string(kv.Key), "/")
177177
name := parts[len(parts)-1]
178-
178+
179179
id := utils.NormalizeAddress(string(kv.Value))
180-
180+
181181
BotMap.Store(name, &BotStatus{
182182
Id: id,
183183
Name: name,
@@ -186,19 +186,19 @@ func InitEtcdRegister() {
186186
LastCheck: time.Now(),
187187
})
188188
}
189-
189+
190190
rch := cli.Watch(ctx, prefix, clientv3.WithPrefix(), clientv3.WithPrevKV())
191-
191+
192192
for wresp := range rch {
193193
for _, ev := range wresp.Events {
194194
key := string(ev.Kv.Key)
195195
val := string(ev.Kv.Value)
196-
196+
197197
switch ev.Type {
198198
case clientv3.EventTypePut:
199199
parts := strings.Split(key, "/")
200200
name := parts[len(parts)-1]
201-
201+
202202
id := utils.NormalizeAddress(val)
203203
BotMap.Store(name, &BotStatus{
204204
Id: id,

admin/conf/conf.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,55 @@ import (
1010
)
1111

1212
type BaseConfig struct {
13-
DBType *string `json:"db_type"`
14-
DBConf *string `json:"db_conf"`
13+
DBType string `json:"db_type"`
14+
DBConf string `json:"db_conf"`
1515

16-
SessionKey *string `json:"session_key"`
16+
SessionKey string `json:"session_key"`
1717

18-
AdminPort *string `json:"admin_port"`
18+
AdminPort string `json:"admin_port"`
1919

20-
CheckBotSec *int `json:"check_bot_sec"`
20+
CheckBotSec int `json:"check_bot_sec"`
2121
}
2222

2323
var BaseConfInfo = new(BaseConfig)
2424

2525
func InitConfig() {
26-
BaseConfInfo.DBType = flag.String("db_type", "sqlite3", "db type")
27-
BaseConfInfo.DBConf = flag.String("db_conf", botUtils.GetAbsPath("data/muse_bot_admin.db"), "db conf")
28-
BaseConfInfo.SessionKey = flag.String("session_key", "muse_bot_session_key", "session key")
29-
BaseConfInfo.AdminPort = flag.String("admin_port", "18080", "admin port")
30-
BaseConfInfo.CheckBotSec = flag.Int("check_bot_sec", 10, "check bot interval")
26+
flag.StringVar(&BaseConfInfo.DBType, "db_type", "sqlite3", "db type")
27+
flag.StringVar(&BaseConfInfo.DBConf, "db_conf", botUtils.GetAbsPath("data/muse_bot_admin.db"), "db conf")
28+
flag.StringVar(&BaseConfInfo.SessionKey, "session_key", "muse_bot_session_key", "session key")
29+
flag.StringVar(&BaseConfInfo.AdminPort, "admin_port", "18080", "admin port")
30+
flag.IntVar(&BaseConfInfo.CheckBotSec, "check_bot_sec", 10, "check bot interval")
3131

3232
InitRegisterConf()
3333

3434
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
3535
flag.Parse()
3636

3737
if os.Getenv("DB_TYPE") != "" {
38-
*BaseConfInfo.DBType = os.Getenv("DB_TYPE")
38+
BaseConfInfo.DBType = os.Getenv("DB_TYPE")
3939
}
4040

4141
if os.Getenv("DB_CONF") != "" {
42-
*BaseConfInfo.DBConf = os.Getenv("DB_CONF")
42+
BaseConfInfo.DBConf = os.Getenv("DB_CONF")
4343
}
4444

4545
if os.Getenv("SESSION_KEY") != "" {
46-
*BaseConfInfo.SessionKey = os.Getenv("SESSION_KEY")
46+
BaseConfInfo.SessionKey = os.Getenv("SESSION_KEY")
4747
}
4848

4949
if os.Getenv("ADMIN_PORT") != "" {
50-
*BaseConfInfo.AdminPort = os.Getenv("ADMIN_PORT")
50+
BaseConfInfo.AdminPort = os.Getenv("ADMIN_PORT")
5151
}
5252

5353
if os.Getenv("CHECK_BOT_SEC") != "" {
54-
*BaseConfInfo.CheckBotSec, _ = strconv.Atoi(os.Getenv("CHECK_BOT_SEC"))
54+
BaseConfInfo.CheckBotSec, _ = strconv.Atoi(os.Getenv("CHECK_BOT_SEC"))
5555
}
5656

57-
logger.Info("CONF", "DBType", *BaseConfInfo.DBType)
58-
logger.Info("CONF", "DBConf", *BaseConfInfo.DBConf)
59-
logger.Info("CONF", "SessionKey", *BaseConfInfo.SessionKey)
60-
logger.Info("CONF", "AdminPort", *BaseConfInfo.AdminPort)
61-
logger.Info("CONF", "CheckBotSec", *BaseConfInfo.CheckBotSec)
57+
logger.Info("CONF", "DBType", BaseConfInfo.DBType)
58+
logger.Info("CONF", "DBConf", BaseConfInfo.DBConf)
59+
logger.Info("CONF", "SessionKey", BaseConfInfo.SessionKey)
60+
logger.Info("CONF", "AdminPort", BaseConfInfo.AdminPort)
61+
logger.Info("CONF", "CheckBotSec", BaseConfInfo.CheckBotSec)
6262

6363
EnvRegisterConf()
6464
}

admin/conf/conf_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ func TestInitConfig_WithEnv(t *testing.T) {
2020

2121
InitConfig()
2222

23-
if *BaseConfInfo.DBType != "mysql" {
24-
t.Errorf("expected DBType = mysql, got %s", *BaseConfInfo.DBType)
23+
if BaseConfInfo.DBType != "mysql" {
24+
t.Errorf("expected DBType = mysql, got %s", BaseConfInfo.DBType)
2525
}
2626

27-
if *BaseConfInfo.DBConf != "root:pass@tcp(localhost:3306)/muse" {
28-
t.Errorf("expected DBConf = mysql conf, got %s", *BaseConfInfo.DBConf)
27+
if BaseConfInfo.DBConf != "root:pass@tcp(localhost:3306)/muse" {
28+
t.Errorf("expected DBConf = mysql conf, got %s", BaseConfInfo.DBConf)
2929
}
3030

31-
if *BaseConfInfo.SessionKey != "env_session_key" {
32-
t.Errorf("expected SessionKey = env_session_key, got %s", *BaseConfInfo.SessionKey)
31+
if BaseConfInfo.SessionKey != "env_session_key" {
32+
t.Errorf("expected SessionKey = env_session_key, got %s", BaseConfInfo.SessionKey)
3333
}
3434

35-
if *BaseConfInfo.AdminPort != "8080" {
36-
t.Errorf("expected AdminPort = 8080, got %s", *BaseConfInfo.AdminPort)
35+
if BaseConfInfo.AdminPort != "8080" {
36+
t.Errorf("expected AdminPort = 8080, got %s", BaseConfInfo.AdminPort)
3737
}
3838

39-
if *BaseConfInfo.CheckBotSec != 99 {
40-
t.Errorf("expected CheckBotSec = 99, got %d", *BaseConfInfo.CheckBotSec)
39+
if BaseConfInfo.CheckBotSec != 99 {
40+
t.Errorf("expected CheckBotSec = 99, got %d", BaseConfInfo.CheckBotSec)
4141
}
4242

4343
if *RegisterConfInfo.Type != "etcd" {

admin/conf/register.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,50 @@ import (
99
)
1010

1111
type RegisterConf struct {
12-
Type *string `json:"type"`
12+
Type string `json:"type"`
1313
EtcdURLs []string `json:"etcd_url"`
14-
EtcdUsername *string `json:"etcd_username"`
15-
EtcdPassword *string `json:"etcd_password"`
14+
EtcdUsername string `json:"etcd_username"`
15+
EtcdPassword string `json:"etcd_password"`
1616

17-
etcdURLs *string
17+
etcdURLs string
1818
}
1919

2020
var (
2121
RegisterConfInfo = new(RegisterConf)
2222
)
2323

2424
func InitRegisterConf() {
25-
RegisterConfInfo.Type = flag.String("register_type", "", "register type: etcd")
26-
RegisterConfInfo.etcdURLs = flag.String("etcd_urls", "", "etcd urls")
27-
RegisterConfInfo.EtcdUsername = flag.String("etcd_username", "", "etcd username")
28-
RegisterConfInfo.EtcdPassword = flag.String("etcd_password", "", "etcd password")
25+
flag.StringVar(&RegisterConfInfo.Type, "register_type", "", "register type: etcd")
26+
flag.StringVar(&RegisterConfInfo.etcdURLs, "etcd_urls", "", "etcd urls")
27+
flag.StringVar(&RegisterConfInfo.EtcdUsername, "etcd_username", "", "etcd username")
28+
flag.StringVar(&RegisterConfInfo.EtcdPassword, "etcd_password", "", "etcd password")
29+
2930
}
3031

3132
func EnvRegisterConf() {
3233
if os.Getenv("REGISTER_TYPE") != "" {
33-
*RegisterConfInfo.Type = os.Getenv("REGISTER_TYPE")
34+
RegisterConfInfo.Type = os.Getenv("REGISTER_TYPE")
3435
}
3536

3637
if os.Getenv("ETCD_URLS") != "" {
3738
RegisterConfInfo.EtcdURLs = strings.Split(os.Getenv("ETCD_URLS"), ",")
3839
}
3940

4041
if os.Getenv("ETCD_USERNAME") != "" {
41-
*RegisterConfInfo.EtcdUsername = os.Getenv("ETCD_USERNAME")
42+
RegisterConfInfo.EtcdUsername = os.Getenv("ETCD_USERNAME")
4243
}
4344

4445
if os.Getenv("ETCD_PASSWORD") != "" {
45-
*RegisterConfInfo.EtcdPassword = os.Getenv("ETCD_PASSWORD")
46+
RegisterConfInfo.EtcdPassword = os.Getenv("ETCD_PASSWORD")
4647
}
4748

48-
if *RegisterConfInfo.Type == "etcd" && *RegisterConfInfo.etcdURLs != "" {
49-
RegisterConfInfo.EtcdURLs = strings.Split(*RegisterConfInfo.etcdURLs, ",")
49+
if RegisterConfInfo.Type == "etcd" && RegisterConfInfo.etcdURLs != "" {
50+
RegisterConfInfo.EtcdURLs = strings.Split(RegisterConfInfo.etcdURLs, ",")
5051
}
5152

52-
logger.Info("REGISTER_CONF", "Type", *RegisterConfInfo.Type)
53+
logger.Info("REGISTER_CONF", "Type", RegisterConfInfo.Type)
5354
logger.Info("REGISTER_CONF", "EtcdURLs", RegisterConfInfo.EtcdURLs)
54-
logger.Info("REGISTER_CONF", "EtcdUsername", *RegisterConfInfo.EtcdUsername)
55-
logger.Info("REGISTER_CONF", "EtcdPassword", *RegisterConfInfo.EtcdPassword)
55+
logger.Info("REGISTER_CONF", "EtcdUsername", RegisterConfInfo.EtcdUsername)
56+
logger.Info("REGISTER_CONF", "EtcdPassword", RegisterConfInfo.EtcdPassword)
5657

5758
}

admin/controller/bot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func ListBots(w http.ResponseWriter, r *http.Request) {
301301
ctx := r.Context()
302302
page, pageSize := parsePaginationParams(r)
303303

304-
if *adminConf.RegisterConfInfo.Type != "" {
304+
if adminConf.RegisterConfInfo.Type != "" {
305305
bots := make([]*RegisterBot, 0)
306306

307307
var total = 0
@@ -551,7 +551,7 @@ func GetAllOnlineBot(w http.ResponseWriter, r *http.Request) {
551551
res := make([]*checkpoint.BotStatus, 0)
552552
checkpoint.BotMap.Range(func(key any, value any) bool {
553553
status := value.(*checkpoint.BotStatus)
554-
if (*adminConf.RegisterConfInfo.Type != "" || status.LastCheck.Add(3*time.Minute).After(time.Now())) &&
554+
if (adminConf.RegisterConfInfo.Type != "" || status.LastCheck.Add(3*time.Minute).After(time.Now())) &&
555555
status.Status != checkpoint.OfflineStatus {
556556
res = append(res, status)
557557
}
@@ -936,7 +936,7 @@ func getBot(r *http.Request) (*db.Bot, error) {
936936
return nil, param.ErrParamError
937937
}
938938

939-
if *adminConf.RegisterConfInfo.Type != "" {
939+
if adminConf.RegisterConfInfo.Type != "" {
940940
return &db.Bot{
941941
Address: idStr,
942942
}, nil

admin/controller/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
)
1818

1919
func InitSessionStore() {
20-
sessionStore = sessions.NewCookieStore([]byte(*conf.BaseConfInfo.SessionKey))
20+
sessionStore = sessions.NewCookieStore([]byte(conf.BaseConfInfo.SessionKey))
2121
}
2222

2323
func UserLogin(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)