diff --git a/control/rule.go b/control/rule.go index a083f240..8e6036ba 100644 --- a/control/rule.go +++ b/control/rule.go @@ -49,8 +49,8 @@ func newctrl(service string, o *Options) *Control { return m } -// enable enables a group to pass the Manager. -func (m *Control) enable(groupID int64) { +// Enable enables a group to pass the Manager. +func (m *Control) Enable(groupID int64) { m.Lock() err := db.Insert(m.service, &grpcfg{groupID, 0}) if err != nil { @@ -59,8 +59,8 @@ func (m *Control) enable(groupID int64) { m.Unlock() } -// disable disables a group to pass the Manager. -func (m *Control) disable(groupID int64) { +// Disable disables a group to pass the Manager. +func (m *Control) Disable(groupID int64) { m.Lock() err := db.Insert(m.service, &grpcfg{groupID, 1}) if err != nil { @@ -69,7 +69,7 @@ func (m *Control) disable(groupID int64) { m.Unlock() } -func (m *Control) isEnabledIn(gid int64) bool { +func (m *Control) IsEnabledIn(gid int64) bool { m.RLock() var c grpcfg err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10)) @@ -81,9 +81,9 @@ func (m *Control) isEnabledIn(gid int64) bool { logrus.Errorf("[control] %v", err) m.RUnlock() if m.options.DisableOnDefault { - m.disable(gid) + m.Disable(gid) } else { - m.enable(gid) + m.Enable(gid) } return !m.options.DisableOnDefault } @@ -92,21 +92,21 @@ func (m *Control) isEnabledIn(gid int64) bool { func (m *Control) Handler() zero.Rule { return func(ctx *zero.Ctx) bool { ctx.State["manager"] = m - return m.isEnabledIn(ctx.Event.GroupID) + return m.IsEnabledIn(ctx.Event.GroupID) } } -// lookup returns a Manager by the service name, if +// Lookup returns a Manager by the service name, if // not exist, it will returns nil. -func lookup(service string) (*Control, bool) { +func Lookup(service string) (*Control, bool) { mu.RLock() defer mu.RUnlock() m, ok := managers[service] return m, ok } -// forEach iterates through managers. -func forEach(iterator func(key string, manager *Control) bool) { +// ForEach iterates through managers. +func ForEach(iterator func(key string, manager *Control) bool) { mu.RLock() m := copyMap(managers) mu.RUnlock() @@ -138,11 +138,11 @@ func init() { Handle(func(ctx *zero.Ctx) { model := extension.CommandModel{} _ = ctx.Parse(&model) - service, ok := lookup(model.Args) + service, ok := Lookup(model.Args) if !ok { ctx.Send("没有找到指定服务!") } - service.enable(ctx.Event.GroupID) + service.Enable(ctx.Event.GroupID) ctx.Send(message.Text("已启用服务: " + model.Args)) }) @@ -150,11 +150,11 @@ func init() { Handle(func(ctx *zero.Ctx) { model := extension.CommandModel{} _ = ctx.Parse(&model) - service, ok := lookup(model.Args) + service, ok := Lookup(model.Args) if !ok { ctx.Send("没有找到指定服务!") } - service.disable(ctx.Event.GroupID) + service.Disable(ctx.Event.GroupID) ctx.Send(message.Text("已关闭服务: " + model.Args)) }) @@ -162,7 +162,7 @@ func init() { Handle(func(ctx *zero.Ctx) { model := extension.CommandModel{} _ = ctx.Parse(&model) - service, ok := lookup(model.Args) + service, ok := Lookup(model.Args) if !ok { ctx.Send("没有找到指定服务!") } @@ -177,10 +177,10 @@ func init() { Handle(func(ctx *zero.Ctx) { msg := `---服务列表---` i := 0 - forEach(func(key string, manager *Control) bool { + ForEach(func(key string, manager *Control) bool { i++ msg += "\n" + strconv.Itoa(i) + `: ` - if manager.isEnabledIn(ctx.Event.GroupID) { + if manager.IsEnabledIn(ctx.Event.GroupID) { msg += "●" + key } else { msg += "○" + key diff --git a/control/gui.go b/control/web/gui.go similarity index 93% rename from control/gui.go rename to control/web/gui.go index 5c3fe959..de448807 100644 --- a/control/gui.go +++ b/control/web/gui.go @@ -1,4 +1,4 @@ -package control +package web import ( "encoding/json" @@ -14,6 +14,7 @@ import ( "github.com/gorilla/websocket" // 前端静态文件 + ctrl "github.com/FloatTech/ZeroBot-Plugin/control" log "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -34,8 +35,8 @@ var ( type logWriter struct { } -// InitGui 初始化gui -func InitGui() { +// initGui 初始化gui +func initGui() { // 将日志重定向到前端hook writer := io.MultiWriter(l, os.Stderr) log.SetOutput(writer) @@ -86,8 +87,8 @@ func controller() { // 获取插件列表 engine.POST("/get_plugins", func(context *gin.Context) { var datas []map[string]interface{} - forEach(func(key string, manager *Control) bool { - datas = append(datas, map[string]interface{}{"id": 1, "handle_type": "", "name": key, "enable": manager.isEnabledIn(0)}) + ctrl.ForEach(func(key string, manager *ctrl.Control) bool { + datas = append(datas, map[string]interface{}{"id": 1, "handle_type": "", "name": key, "enable": manager.IsEnabledIn(0)}) return true }) context.JSON(200, datas) @@ -134,14 +135,14 @@ func updateAllPluginStatus(context *gin.Context) { return true }) - forEach(func(key string, manager *Control) bool { + ctrl.ForEach(func(key string, manager *ctrl.Control) bool { if enable { for _, group := range groups { - manager.enable(group) + manager.Enable(group) } } else { for _, group := range groups { - manager.disable(group) + manager.Disable(group) } } return true @@ -168,7 +169,7 @@ func updatePluginAllGroupStatus(context *gin.Context) { name = parse["name"].(string) enable = parse["enable"].(bool) } - control, b := lookup(name) + control, b := ctrl.Lookup(name) if !b { context.JSON(404, nil) return @@ -176,9 +177,9 @@ func updatePluginAllGroupStatus(context *gin.Context) { zero.RangeBot(func(id int64, ctx *zero.Ctx) bool { for _, group := range ctx.GetGroupList().Array() { if enable { - control.enable(group.Get("group_id").Int()) + control.Enable(group.Get("group_id").Int()) } else { - control.disable(group.Get("group_id").Int()) + control.Disable(group.Get("group_id").Int()) } } @@ -205,15 +206,15 @@ func updatePluginStatus(context *gin.Context) { name := parse["name"].(string) enable := parse["enable"].(bool) fmt.Println(name) - control, b := lookup(name) + control, b := ctrl.Lookup(name) if !b { context.JSON(404, "服务不存在") return } if enable { - control.enable(groupID) + control.Enable(groupID) } else { - control.disable(groupID) + control.Disable(groupID) } context.JSON(200, nil) } @@ -237,12 +238,12 @@ func getPluginStatus(context *gin.Context) { groupID = int64(parse["group_id"].(float64)) name = parse["name"].(string) } - control, b := lookup(name) + control, b := ctrl.Lookup(name) if !b { context.JSON(404, "服务不存在") return } - context.JSON(200, gin.H{"enable": control.isEnabledIn(groupID)}) + context.JSON(200, gin.H{"enable": control.IsEnabledIn(groupID)}) } // getPluginsStatus @@ -263,8 +264,8 @@ func getPluginsStatus(context *gin.Context) { groupID = int64(parse["group_id"].(float64)) } var datas []map[string]interface{} - forEach(func(key string, manager *Control) bool { - enable := manager.isEnabledIn(groupID) + ctrl.ForEach(func(key string, manager *ctrl.Control) bool { + enable := manager.IsEnabledIn(groupID) datas = append(datas, map[string]interface{}{"name": key, "enable": enable}) return true }) diff --git a/control/web/init.go b/control/web/init.go new file mode 100644 index 00000000..95c9c613 --- /dev/null +++ b/control/web/init.go @@ -0,0 +1,13 @@ +// Package web 网页管理后端 +package web + +import "flag" + +func init() { + var en bool + // 解析命令行参数,输入 `-g` 即可启用 gui + flag.BoolVar(&en, "g", false, "Enable web gui.") + if en { + initGui() + } +} diff --git a/main.go b/main.go index 155efaff..bbe9c15b 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,9 @@ import ( // 注:以下插件均可通过前面加 // 注释,注释后停用并不加载插件 // 下列插件可与 wdvxdr1123/ZeroBot v1.1.2 以上配合单独使用 + // 插件控制 + _ "github.com/FloatTech/ZeroBot-Plugin/control/web" // web 后端控制 // 词库类 - "github.com/sirupsen/logrus" - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_atri" // ATRI词库 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat" // 基础词库 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_qingyunke" // 青云客 @@ -45,11 +45,9 @@ import ( _ "github.com/FloatTech/ZeroBot-Plugin/plugin_setutime" // 来份涩图 // 以下为内置依赖,勿动 - + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/driver" - - "github.com/FloatTech/ZeroBot-Plugin/control" ) var ( @@ -63,16 +61,14 @@ var ( ) func init() { - var en bool var debg bool + /* 注释处已移动至 control/web // 解析命令行参数,输入 `-g` 即可启用 gui flag.BoolVar(&en, "g", false, "Enable web gui.") + */ // 解析命令行参数,输入 `-d` 即可开启 debug log flag.BoolVar(&debg, "d", false, "Enable debug log.") flag.Parse() - if en { - control.InitGui() - } if debg { logrus.SetLevel(logrus.DebugLevel) }