1
0
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:
wdvxdr 2021-04-12 18:38:21 +08:00
parent d8a373cfa4
commit b9e7006547
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6
4 changed files with 113 additions and 86 deletions

View File

@ -33,14 +33,14 @@ linters:
#- goconst
- gocritic
#- gocyclo
- gofumpt
- gofmt
- goimports
- goprintffuncname
#- gosec
- gosimple
- govet
- ineffassign
- misspell
#- misspell
- nolintlint
- rowserrcheck
- staticcheck

View File

@ -2,18 +2,21 @@
package config
import (
"bufio"
_ "embed" // embed the default config file
"fmt"
"os"
"path"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// DefaultConfig 默认配置文件
// defaultConfig 默认配置文件
//go:embed default_config.yml
var DefaultConfig string
var defaultConfig string
var currentPath = getCurrentPath()
@ -29,7 +32,7 @@ type Config struct {
Status int32 `yaml:"status"`
ReLogin struct {
Disabled bool `yaml:"disabled"`
Delay int `yaml:"delay"`
Delay uint `yaml:"delay"`
MaxTimes uint `yaml:"max-times"`
Interval int `yaml:"interval"`
}
@ -128,8 +131,8 @@ func Get() *Config {
once.Do(func() {
file, err := os.Open(DefaultConfigFile)
if err != nil {
log.Error("获取配置文件失败: ", err)
return
generateConfig()
os.Exit(0)
}
defer file.Close()
config = &Config{}
@ -148,3 +151,92 @@ func getCurrentPath() string {
}
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
`

View File

@ -6,9 +6,8 @@ account: # 账号相关
encrypt: false # 是否开启密码加密
status: 0 # 在线状态 请参考 https://github.com/Mrs4s/go-cqhttp/blob/dev/docs/config.md#在线状态
relogin: # 重连设置
disabled: false
delay: 3 # 重连延迟, 单位秒
interval: 0 # 重连间隔
delay: 3 # 首次重连延迟, 单位秒
interval: 3 # 重连间隔
max-times: 0 # 最大重连次数, 0为无限制
# 是否使用服务器下发的新地址进行重连
@ -16,7 +15,6 @@ account: # 账号相关
use-sso-address: true
heartbeat:
disabled: false # 是否开启心跳事件上报
# 心跳频率, 单位秒
# -1 为关闭心跳
interval: 5
@ -64,72 +62,17 @@ default-middlewares: &default
frequency: 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: # 数据库相关设置
leveldb:
# 是否启用内置leveldb数据库
# 启用将会增加10-20MB的内存占用和一定的磁盘空间
# 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能
enable: true
# 连接服务列表
servers:
# 添加方式,同一连接方式可添加多个,具体配置说明请查看文档
#- http: # http 通信
#- ws: # 正向 Websocket
#- ws-reverse: # 反向 Websocket
#- pprof: #性能分析服务器

16
main.go
View File

@ -17,8 +17,6 @@ import (
"syscall"
"time"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/global/config"
@ -26,6 +24,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global/update"
"github.com/Mrs4s/go-cqhttp/server"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/guonaihong/gout"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
@ -67,6 +66,7 @@ func init() {
TimestampFormat: "2006-01-02 15:04:05",
LogFormat: "[%time%] [%lvl%]: %msg% \n",
}
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotatelogs.WithRotationTime(time.Hour*24))
if err != nil {
log.Errorf("rotatelogs init err: %v", err)
@ -74,13 +74,6 @@ func init() {
}
conf = config.Get()
if conf == nil {
_ = os.WriteFile("config.yml", []byte(config.DefaultConfig), 0644)
log.Error("未找到配置文件,默认配置文件已生成!")
readLine()
os.Exit(0)
}
if debug {
conf.Output.Debug = true
}
@ -88,7 +81,6 @@ func init() {
if conf.Output.Debug {
log.SetReportCaller(true)
}
log.AddHook(global.NewLocalHook(w, logFormatter, global.GetLogLevel(conf.Output.LogLevel)...))
if !global.PathExists(global.ImagePath) {
@ -411,7 +403,7 @@ func main() {
if c, ok := m["ws-reverse"]; ok {
rc := new(config.WebsocketReverse)
if err := c.Decode(rc); err != nil {
log.Warn("读取http配置失败 :", err)
log.Warn("读取正向Websocket配置失败 :", err)
} else {
go server.RunWebSocketClient(bot, rc)
}
@ -419,7 +411,7 @@ func main() {
if p, ok := m["pprof"]; ok {
pc := new(config.PprofServer)
if err := p.Decode(pc); err != nil {
log.Warn("读取http配置失败 :", err)
log.Warn("读取反向Websocket配置失败 :", err)
} else {
go server.RunPprofServer(pc)
}