Skip to content

Commit

Permalink
chore:修改配置加载库,为后续热更新做准备
Browse files Browse the repository at this point in the history
  • Loading branch information
Azusa-Yuan committed Jan 4, 2025
1 parent 726badc commit c9f9781
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
26 changes: 13 additions & 13 deletions ZeroBot/bot_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import "time"

// Config is config of zero bot
type Config struct {
NickName []string `json:"nickname" yaml:"nickname"` // 机器人名称
CommandPrefix string `json:"command_prefix" yaml:"command_prefix"` // 触发命令
SuperUsers []int64 `json:"super_users" yaml:"super_users"` // 超级用户
RingLen uint `json:"ring_len" yaml:"ring_len"` // 事件环长度 (默认关闭)
Latency time.Duration `json:"latency" yaml:"latency"` // 事件处理延迟 (延迟 latency 再处理事件,在 ring 模式下不可低于 1ms)
MaxProcessTime time.Duration `json:"max_process_time" yaml:"max_process_time"` // 事件最大处理时间 (默认4min)
MarkMessage bool `json:"mark_message" yaml:"mark_message"` // 自动标记消息为已读
Driver []Driver `json:"-" yaml:"-"` // 通信驱动
InstanceMap map[int64]Instance `json:"instance" yaml:"instance"`
NickName []string `json:"nickname" yaml:"nickname" mapstructure:"nickname"` // 机器人名称
CommandPrefix string `json:"command_prefix" yaml:"command_prefix" mapstructure:"command_prefix"` // 触发命令
SuperUsers []int64 `json:"super_users" yaml:"super_users" mapstructure:"super_users"` // 超级用户
RingLen uint `json:"ring_len" yaml:"ring_len" mapstructure:"ring_len"` // 事件环长度 (默认关闭)
Latency time.Duration `json:"latency" yaml:"latency" mapstructure:"latency"` // 事件处理延迟 (延迟 latency 再处理事件,在 ring 模式下不可低于 1ms)
MaxProcessTime time.Duration `json:"max_process_time" yaml:"max_process_time" mapstructure:"max_process_time"` // 事件最大处理时间 (默认4min)
MarkMessage bool `json:"mark_message" yaml:"mark_message" mapstructure:"mark_message"` // 自动标记消息为已读
Driver []Driver `json:"-" yaml:"-"` // 通信驱动
InstanceMap map[int64]Instance `json:"instance" yaml:"instance" mapstructure:"instance"`
}

// BotConfig 运行中bot的配置
var BotConfig Config

type Instance struct {
SelfID int64 `json:"seld_id"`
NickName []string `json:"nickname" yaml:"nickname"` // 机器人名称
CommandPrefix string `json:"command_prefix" yaml:"command_prefix"` // 触发命令
SuperUsers []int64 `json:"super_users" yaml:"super_users"` // 超级用户
SelfID int64 `json:"seld_id" mapstructure:"seld_id"` // 机器人QQ号
NickName []string `json:"nickname" yaml:"nickname" mapstructure:"nickname"` // 机器人名称
CommandPrefix string `json:"command_prefix" yaml:"command_prefix" mapstructure:"command_prefix"` // 触发命令
SuperUsers []int64 `json:"super_users" yaml:"super_users" mapstructure:"super_users"` // 超级用户
}

func (c Config) GetNickName(selfID int64) []string {
Expand Down
27 changes: 15 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"MiniBot/utils/path"
zero "ZeroBot"
"os"
"path/filepath"

"ZeroBot/driver"

"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
"github.com/spf13/viper"
)

var Config MiniConfig
Expand All @@ -21,7 +20,7 @@ type ConnConfig struct {

// 对应config.json
type MiniConfig struct {
Z zero.Config `json:"zero" yaml:"zero"`
Z zero.Config `json:"zero" yaml:"zero" mapstructure:"zero"`
WS []ConnConfig `json:"ws" yaml:"ws"`
WSS []ConnConfig `json:"wss" yaml:"wss"`
// C []*driver.WSClient `json:"-" yaml:"-"`
Expand All @@ -30,21 +29,25 @@ type MiniConfig struct {

func ConfigInit() {
// Read YAML file
configPath := filepath.Join(path.ConfPath, "config.yaml")
configName := "config"
if os.Getenv("ENVIRONMENT") == "dev" {
configPath = filepath.Join(path.ConfPath, "config_dev.yaml")
configName = "config_dev"
log.Info().Msg("目前处于开发环境,请注意主目录下所有配置文件的内容和路径是否正确")
}
data, err := os.ReadFile(configPath)
if err != nil {
log.Fatal().Msgf("error reading config file: %v", err)

viper.SetConfigName(configName) // 配置文件名称(无后缀)
viper.AddConfigPath(path.ConfPath) // 配置文件路径
viper.SetConfigType("yaml") // 配置文件后缀, 也可以是 json, toml, yaml, yml 等, 不设置则自动识别

// 读取配置文件, 如果出错则退出
if err := viper.ReadInConfig(); err != nil {
log.Fatal().Msgf("Error reading config file, %s\n", err)
}

// Unmarshal YAML data
err = yaml.Unmarshal(data, &Config)
if err != nil {
log.Fatal().Msgf("error unmarshalling config file: %v", err)
if err := viper.Unmarshal(&Config); err != nil {
log.Fatal().Msgf("Error reading config file, %s\n", err)
}

for _, client := range Config.WS {
Config.Z.Driver = append(Config.Z.Driver, driver.NewWebSocketClient(client.URL, client.AccessToken))
}
Expand Down
32 changes: 32 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
zero:
nickname:
- 露露姆
command_prefix: "/"
super_users:
- 1043728417
ring_len: 1024
latency: 100000000
max_process_time: 240000000000
# 是否进行消息已读,调试时可以false更方便
mark_message: false
instance:
721223121:
nickname:
- 白洲梓
- 小梓
super_users:
- 1043728417
command_prefix: "/"

# 正向websocket,可以理解为Minibot作为websocket客户端
ws:
- url: "ws://127.0.0.1:6700"
access_token: ""
# - url: "ws://127.0.0.1:6800"
# access_token: ""

# 反向websocket,可以理解为Minibot作为websocket服务端,一个服务端可以接多个客户端,不建议设置多个wss
# 0.0.0.0 是调试环境所用,生产环境不建议这么配置
wss:
- url: "ws://127.0.0.1:6900"
# access_token: ""
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/prometheus/client_golang v1.20.2
github.com/redis/go-redis/v9 v9.6.1
github.com/rs/zerolog v1.33.0
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.18.0
github.com/vmihailenco/msgpack/v5 v5.4.1
Expand Down Expand Up @@ -80,6 +81,7 @@ require (
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4 // indirect
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fumiama/go-base16384 v1.7.0 // indirect
github.com/fumiama/go-simple-protobuf v0.2.0 // indirect
github.com/fumiama/gofastTEA v0.0.10 // indirect
Expand All @@ -102,6 +104,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
Expand All @@ -113,19 +116,27 @@ require (
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 // indirect
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tebeka/strftime v0.1.5 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
Expand All @@ -150,6 +161,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo=
github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY=
Expand Down Expand Up @@ -297,6 +298,7 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
Expand Down Expand Up @@ -358,6 +360,7 @@ github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f h1:sgU
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f/go.mod h1:UGmTpUd3rjbtfIpwAPrcfmGf/Z1HS95TATB+m57TPB8=
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 h1:Bvq8AziQ5jFF4BHGAEDSqwPW1NJS3XshxbRCxtjFAZc=
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042/go.mod h1:TPpsiPUEh0zFL1Snz4crhMlBe60PYxRHr5oFF3rRYg0=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
Expand All @@ -379,6 +382,7 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
Expand All @@ -396,6 +400,7 @@ github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzb
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
Expand Down Expand Up @@ -440,11 +445,16 @@ github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYl
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44=
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand All @@ -462,6 +472,7 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tebeka/strftime v0.1.5 h1:1NQKN1NiQgkqd/2moD6ySP/5CoZQsKa1d3ZhJ44Jpmg=
github.com/tebeka/strftime v0.1.5/go.mod h1:29/OidkoWHdEKZqzyDLUyC+LmgDgdHo4WAFCDT7D/Ig=
Expand Down Expand Up @@ -896,6 +907,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit c9f9781

Please sign in to comment.