Skip to content

Commit

Permalink
✏️ 解耦 web gui
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 10, 2021
1 parent 3b3dd3d commit 8c9ced0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
38 changes: 19 additions & 19 deletions control/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
Expand All @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -138,31 +138,31 @@ 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))
})

zero.OnCommandGroup([]string{"禁用", "disable"}, zero.AdminPermission, zero.OnlyGroup).
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))
})

zero.OnCommandGroup([]string{"用法", "usage"}, zero.AdminPermission, zero.OnlyGroup).
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("没有找到指定服务!")
}
Expand All @@ -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
Expand Down
37 changes: 19 additions & 18 deletions control/gui.go → control/web/gui.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package control
package web

import (
"encoding/json"
Expand All @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -168,17 +169,17 @@ 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
}
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())
}
}

Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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
})
Expand Down
13 changes: 13 additions & 0 deletions control/web/init.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
14 changes: 5 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" // 青云客
Expand Down Expand Up @@ -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 (
Expand All @@ -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)
}
Expand Down

0 comments on commit 8c9ced0

Please sign in to comment.