mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
feat(config): config init helper
简单向导
This commit is contained in:
parent
d8a373cfa4
commit
b9e7006547
@ -33,14 +33,14 @@ linters:
|
|||||||
#- goconst
|
#- goconst
|
||||||
- gocritic
|
- gocritic
|
||||||
#- gocyclo
|
#- gocyclo
|
||||||
- gofumpt
|
- gofmt
|
||||||
- goimports
|
- goimports
|
||||||
- goprintffuncname
|
- goprintffuncname
|
||||||
#- gosec
|
#- gosec
|
||||||
- gosimple
|
- gosimple
|
||||||
- govet
|
- govet
|
||||||
- ineffassign
|
- ineffassign
|
||||||
- misspell
|
#- misspell
|
||||||
- nolintlint
|
- nolintlint
|
||||||
- rowserrcheck
|
- rowserrcheck
|
||||||
- staticcheck
|
- staticcheck
|
||||||
|
@ -2,18 +2,21 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
_ "embed" // embed the default config file
|
_ "embed" // embed the default config file
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultConfig 默认配置文件
|
// defaultConfig 默认配置文件
|
||||||
//go:embed default_config.yml
|
//go:embed default_config.yml
|
||||||
var DefaultConfig string
|
var defaultConfig string
|
||||||
|
|
||||||
var currentPath = getCurrentPath()
|
var currentPath = getCurrentPath()
|
||||||
|
|
||||||
@ -29,7 +32,7 @@ type Config struct {
|
|||||||
Status int32 `yaml:"status"`
|
Status int32 `yaml:"status"`
|
||||||
ReLogin struct {
|
ReLogin struct {
|
||||||
Disabled bool `yaml:"disabled"`
|
Disabled bool `yaml:"disabled"`
|
||||||
Delay int `yaml:"delay"`
|
Delay uint `yaml:"delay"`
|
||||||
MaxTimes uint `yaml:"max-times"`
|
MaxTimes uint `yaml:"max-times"`
|
||||||
Interval int `yaml:"interval"`
|
Interval int `yaml:"interval"`
|
||||||
}
|
}
|
||||||
@ -128,8 +131,8 @@ func Get() *Config {
|
|||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
file, err := os.Open(DefaultConfigFile)
|
file, err := os.Open(DefaultConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("获取配置文件失败: ", err)
|
generateConfig()
|
||||||
return
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
config = &Config{}
|
config = &Config{}
|
||||||
@ -148,3 +151,92 @@ func getCurrentPath() string {
|
|||||||
}
|
}
|
||||||
return cwd
|
return cwd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateConfig 生成配置文件
|
||||||
|
func generateConfig() {
|
||||||
|
fmt.Println("未找到配置文件,正在为您生成配置文件中!")
|
||||||
|
sb := strings.Builder{}
|
||||||
|
sb.WriteString(defaultConfig)
|
||||||
|
fmt.Print(`请选择你需要的通信方式:
|
||||||
|
> 1: HTTP通信
|
||||||
|
> 2: 正向 Websocket 通信
|
||||||
|
> 3: 反向 Websocket 通信
|
||||||
|
> 4: pprof 性能分析服务器
|
||||||
|
请输入你需要的编号,可输入多个,同一编号也可输入多个(如: 233)
|
||||||
|
您的选择是:`)
|
||||||
|
input := bufio.NewReader(os.Stdin)
|
||||||
|
readString, err := input.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("输入不合法: ", err)
|
||||||
|
}
|
||||||
|
for _, r := range readString {
|
||||||
|
switch r {
|
||||||
|
case '1':
|
||||||
|
sb.WriteString(httpDefault)
|
||||||
|
case '2':
|
||||||
|
sb.WriteString(wsDefault)
|
||||||
|
case '3':
|
||||||
|
sb.WriteString(wsReverseDefault)
|
||||||
|
case '4':
|
||||||
|
sb.WriteString(pprofDefault)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = os.WriteFile("config.yml", []byte(sb.String()), 0644)
|
||||||
|
fmt.Println("默认配置文件已生成,请修改 config.yml 后重新启动!")
|
||||||
|
_, _ = input.ReadString('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
const httpDefault = ` # HTTP 通信设置
|
||||||
|
- http:
|
||||||
|
# 服务端监听地址
|
||||||
|
host: 127.0.0.1
|
||||||
|
# 服务端监听端口
|
||||||
|
port: 5700
|
||||||
|
# 反向HTTP超时时间, 单位秒
|
||||||
|
# 最小值为5,小于5将会忽略本项设置
|
||||||
|
timeout: 5
|
||||||
|
middlewares:
|
||||||
|
<<: *default # 引用默认中间件
|
||||||
|
# 反向HTTP POST地址列表
|
||||||
|
post:
|
||||||
|
#- url: '' # 地址
|
||||||
|
# secret: '' # 密钥
|
||||||
|
#- url: 127.0.0.1:5701 # 地址
|
||||||
|
# secret: '' # 密钥
|
||||||
|
`
|
||||||
|
|
||||||
|
const wsDefault = ` # 正向WS设置
|
||||||
|
- ws:
|
||||||
|
# 正向WS服务器监听地址
|
||||||
|
host: 127.0.0.1
|
||||||
|
# 正向WS服务器监听端口
|
||||||
|
port: 6700
|
||||||
|
middlewares:
|
||||||
|
<<: *default # 引用默认中间件
|
||||||
|
`
|
||||||
|
|
||||||
|
const wsReverseDefault = ` - ws-reverse:
|
||||||
|
# 反向WS Universal 地址
|
||||||
|
# 注意 设置了此项地址后下面两项将会被忽略
|
||||||
|
universal: ws://your_websocket_universal.server
|
||||||
|
# 反向WS API 地址
|
||||||
|
api: ws://your_websocket_api.server
|
||||||
|
# 反向WS Event 地址
|
||||||
|
event: ws://your_websocket_event.server
|
||||||
|
# 重连间隔 单位毫秒
|
||||||
|
reconnect-interval: 3000
|
||||||
|
middlewares:
|
||||||
|
<<: *default # 引用默认中间件
|
||||||
|
`
|
||||||
|
|
||||||
|
const pprofDefault = ` # pprof 性能分析服务器, 一般情况下不需要启用.
|
||||||
|
# 如果遇到性能问题请上传报告给开发者处理
|
||||||
|
# 注意: pprof服务不支持中间件、不支持鉴权. 请不要开放到公网
|
||||||
|
- pprof:
|
||||||
|
# 是否禁用pprof性能分析服务器
|
||||||
|
disabled: true
|
||||||
|
# pprof服务器监听地址
|
||||||
|
host: 127.0.0.1
|
||||||
|
# pprof服务器监听端口
|
||||||
|
port: 7700
|
||||||
|
`
|
||||||
|
@ -6,9 +6,8 @@ account: # 账号相关
|
|||||||
encrypt: false # 是否开启密码加密
|
encrypt: false # 是否开启密码加密
|
||||||
status: 0 # 在线状态 请参考 https://github.com/Mrs4s/go-cqhttp/blob/dev/docs/config.md#在线状态
|
status: 0 # 在线状态 请参考 https://github.com/Mrs4s/go-cqhttp/blob/dev/docs/config.md#在线状态
|
||||||
relogin: # 重连设置
|
relogin: # 重连设置
|
||||||
disabled: false
|
delay: 3 # 首次重连延迟, 单位秒
|
||||||
delay: 3 # 重连延迟, 单位秒
|
interval: 3 # 重连间隔
|
||||||
interval: 0 # 重连间隔
|
|
||||||
max-times: 0 # 最大重连次数, 0为无限制
|
max-times: 0 # 最大重连次数, 0为无限制
|
||||||
|
|
||||||
# 是否使用服务器下发的新地址进行重连
|
# 是否使用服务器下发的新地址进行重连
|
||||||
@ -16,7 +15,6 @@ account: # 账号相关
|
|||||||
use-sso-address: true
|
use-sso-address: true
|
||||||
|
|
||||||
heartbeat:
|
heartbeat:
|
||||||
disabled: false # 是否开启心跳事件上报
|
|
||||||
# 心跳频率, 单位秒
|
# 心跳频率, 单位秒
|
||||||
# -1 为关闭心跳
|
# -1 为关闭心跳
|
||||||
interval: 5
|
interval: 5
|
||||||
@ -64,72 +62,17 @@ default-middlewares: &default
|
|||||||
frequency: 1 # 令牌回复频率, 单位秒
|
frequency: 1 # 令牌回复频率, 单位秒
|
||||||
bucket: 1 # 令牌桶大小
|
bucket: 1 # 令牌桶大小
|
||||||
|
|
||||||
servers:
|
|
||||||
# HTTP 通信设置
|
|
||||||
- http:
|
|
||||||
# 是否关闭正向HTTP服务器
|
|
||||||
disabled: false
|
|
||||||
# 服务端监听地址
|
|
||||||
host: 127.0.0.1
|
|
||||||
# 服务端监听端口
|
|
||||||
port: 5700
|
|
||||||
# 反向HTTP超时时间, 单位秒
|
|
||||||
# 最小值为5,小于5将会忽略本项设置
|
|
||||||
timeout: 5
|
|
||||||
middlewares:
|
|
||||||
<<: *default # 引用默认中间件
|
|
||||||
# 反向HTTP POST地址列表
|
|
||||||
post:
|
|
||||||
#- url: '' # 地址
|
|
||||||
# secret: '' # 密钥
|
|
||||||
#- url: 127.0.0.1:5701 # 地址
|
|
||||||
# secret: '' # 密钥
|
|
||||||
|
|
||||||
# 正向WS设置
|
|
||||||
- ws:
|
|
||||||
# 是否禁用正向WS服务器
|
|
||||||
disabled: true
|
|
||||||
# 正向WS服务器监听地址
|
|
||||||
host: 127.0.0.1
|
|
||||||
# 正向WS服务器监听端口
|
|
||||||
port: 6700
|
|
||||||
middlewares:
|
|
||||||
<<: *default # 引用默认中间件
|
|
||||||
|
|
||||||
- ws-reverse:
|
|
||||||
# 是否禁用当前反向WS服务
|
|
||||||
disabled: true
|
|
||||||
# 反向WS Universal 地址
|
|
||||||
# 注意 设置了此项地址后下面两项将会被忽略
|
|
||||||
universal: ws://your_websocket_universal.server
|
|
||||||
# 反向WS API 地址
|
|
||||||
api: ws://your_websocket_api.server
|
|
||||||
# 反向WS Event 地址
|
|
||||||
event: ws://your_websocket_event.server
|
|
||||||
# 重连间隔 单位毫秒
|
|
||||||
reconnect-interval: 3000
|
|
||||||
middlewares:
|
|
||||||
<<: *default # 引用默认中间件
|
|
||||||
# pprof 性能分析服务器, 一般情况下不需要启用.
|
|
||||||
# 如果遇到性能问题请上传报告给开发者处理
|
|
||||||
# 注意: pprof服务不支持中间件、不支持鉴权. 请不要开放到公网
|
|
||||||
- pprof:
|
|
||||||
# 是否禁用pprof性能分析服务器
|
|
||||||
disabled: true
|
|
||||||
# pprof服务器监听地址
|
|
||||||
host: 127.0.0.1
|
|
||||||
# pprof服务器监听端口
|
|
||||||
port: 7700
|
|
||||||
|
|
||||||
# 可添加更多
|
|
||||||
#- ws-reverse:
|
|
||||||
#- ws:
|
|
||||||
#- http:
|
|
||||||
#- pprof:
|
|
||||||
|
|
||||||
database: # 数据库相关设置
|
database: # 数据库相关设置
|
||||||
leveldb:
|
leveldb:
|
||||||
# 是否启用内置leveldb数据库
|
# 是否启用内置leveldb数据库
|
||||||
# 启用将会增加10-20MB的内存占用和一定的磁盘空间
|
# 启用将会增加10-20MB的内存占用和一定的磁盘空间
|
||||||
# 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能
|
# 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能
|
||||||
enable: true
|
enable: true
|
||||||
|
|
||||||
|
# 连接服务列表
|
||||||
|
servers:
|
||||||
|
# 添加方式,同一连接方式可添加多个,具体配置说明请查看文档
|
||||||
|
#- http: # http 通信
|
||||||
|
#- ws: # 正向 Websocket
|
||||||
|
#- ws-reverse: # 反向 Websocket
|
||||||
|
#- pprof: #性能分析服务器
|
||||||
|
16
main.go
16
main.go
@ -17,8 +17,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||||
@ -26,6 +24,7 @@ import (
|
|||||||
"github.com/Mrs4s/go-cqhttp/global/update"
|
"github.com/Mrs4s/go-cqhttp/global/update"
|
||||||
"github.com/Mrs4s/go-cqhttp/server"
|
"github.com/Mrs4s/go-cqhttp/server"
|
||||||
|
|
||||||
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
"github.com/Mrs4s/MiraiGo/client"
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
"github.com/guonaihong/gout"
|
"github.com/guonaihong/gout"
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
@ -67,6 +66,7 @@ func init() {
|
|||||||
TimestampFormat: "2006-01-02 15:04:05",
|
TimestampFormat: "2006-01-02 15:04:05",
|
||||||
LogFormat: "[%time%] [%lvl%]: %msg% \n",
|
LogFormat: "[%time%] [%lvl%]: %msg% \n",
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotatelogs.WithRotationTime(time.Hour*24))
|
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotatelogs.WithRotationTime(time.Hour*24))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("rotatelogs init err: %v", err)
|
log.Errorf("rotatelogs init err: %v", err)
|
||||||
@ -74,13 +74,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
conf = config.Get()
|
conf = config.Get()
|
||||||
if conf == nil {
|
|
||||||
_ = os.WriteFile("config.yml", []byte(config.DefaultConfig), 0644)
|
|
||||||
log.Error("未找到配置文件,默认配置文件已生成!")
|
|
||||||
readLine()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
conf.Output.Debug = true
|
conf.Output.Debug = true
|
||||||
}
|
}
|
||||||
@ -88,7 +81,6 @@ func init() {
|
|||||||
if conf.Output.Debug {
|
if conf.Output.Debug {
|
||||||
log.SetReportCaller(true)
|
log.SetReportCaller(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.AddHook(global.NewLocalHook(w, logFormatter, global.GetLogLevel(conf.Output.LogLevel)...))
|
log.AddHook(global.NewLocalHook(w, logFormatter, global.GetLogLevel(conf.Output.LogLevel)...))
|
||||||
|
|
||||||
if !global.PathExists(global.ImagePath) {
|
if !global.PathExists(global.ImagePath) {
|
||||||
@ -411,7 +403,7 @@ func main() {
|
|||||||
if c, ok := m["ws-reverse"]; ok {
|
if c, ok := m["ws-reverse"]; ok {
|
||||||
rc := new(config.WebsocketReverse)
|
rc := new(config.WebsocketReverse)
|
||||||
if err := c.Decode(rc); err != nil {
|
if err := c.Decode(rc); err != nil {
|
||||||
log.Warn("读取http配置失败 :", err)
|
log.Warn("读取正向Websocket配置失败 :", err)
|
||||||
} else {
|
} else {
|
||||||
go server.RunWebSocketClient(bot, rc)
|
go server.RunWebSocketClient(bot, rc)
|
||||||
}
|
}
|
||||||
@ -419,7 +411,7 @@ func main() {
|
|||||||
if p, ok := m["pprof"]; ok {
|
if p, ok := m["pprof"]; ok {
|
||||||
pc := new(config.PprofServer)
|
pc := new(config.PprofServer)
|
||||||
if err := p.Decode(pc); err != nil {
|
if err := p.Decode(pc); err != nil {
|
||||||
log.Warn("读取http配置失败 :", err)
|
log.Warn("读取反向Websocket配置失败 :", err)
|
||||||
} else {
|
} else {
|
||||||
go server.RunPprofServer(pc)
|
go server.RunPprofServer(pc)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user