✏️ 修复首次启动可能 panic

This commit is contained in:
fumiama 2021-09-10 14:00:46 +08:00
parent b02789b560
commit 308ec05405
2 changed files with 23 additions and 20 deletions

View File

@ -6,6 +6,6 @@ import (
func Register(service string, o *Options) *zero.Engine { func Register(service string, o *Options) *zero.Engine {
engine := zero.New() engine := zero.New()
engine.UsePreHandler(New(service, o).Handler()) engine.UsePreHandler(new(service, o).Handler())
return engine return engine
} }

View File

@ -28,8 +28,8 @@ type Control struct {
options Options options Options
} }
// New returns Manager with settings. // new returns Manager with settings.
func New(service string, o *Options) *Control { func new(service string, o *Options) *Control {
m := &Control{service: service, m := &Control{service: service,
options: func() Options { options: func() Options {
if o == nil { if o == nil {
@ -48,8 +48,8 @@ func New(service string, o *Options) *Control {
return m return m
} }
// Enable enables a group to pass the Manager. // enable enables a group to pass the Manager.
func (m *Control) Enable(groupID int64) { func (m *Control) enable(groupID int64) {
m.Lock() m.Lock()
err := db.Insert(m.service, &grpcfg{groupID, 0}) err := db.Insert(m.service, &grpcfg{groupID, 0})
if err != nil { if err != nil {
@ -58,8 +58,8 @@ func (m *Control) Enable(groupID int64) {
m.Unlock() m.Unlock()
} }
// Disable disables a group to pass the Manager. // disable disables a group to pass the Manager.
func (m *Control) Disable(groupID int64) { func (m *Control) disable(groupID int64) {
m.Lock() m.Lock()
err := db.Insert(m.service, &grpcfg{groupID, 1}) err := db.Insert(m.service, &grpcfg{groupID, 1})
if err != nil { if err != nil {
@ -84,25 +84,25 @@ func (m *Control) Handler() zero.Rule {
} }
m.RUnlock() m.RUnlock()
if m.options.DisableOnDefault { if m.options.DisableOnDefault {
m.Disable(ctx.Event.GroupID) m.disable(ctx.Event.GroupID)
} else { } else {
m.Enable(ctx.Event.GroupID) m.enable(ctx.Event.GroupID)
} }
return !m.options.DisableOnDefault return !m.options.DisableOnDefault
} }
} }
// Lookup returns a Manager by the service name, if // lookup returns a Manager by the service name, if
// not exist, it will returns nil. // not exist, it will returns nil.
func Lookup(service string) (*Control, bool) { func lookup(service string) (*Control, bool) {
mu.RLock() mu.RLock()
defer mu.RUnlock() defer mu.RUnlock()
m, ok := managers[service] m, ok := managers[service]
return m, ok return m, ok
} }
// ForEach iterates through managers. // forEach iterates through managers.
func ForEach(iterator func(key string, manager *Control) bool) { func forEach(iterator func(key string, manager *Control) bool) {
mu.RLock() mu.RLock()
m := copyMap(managers) m := copyMap(managers)
mu.RUnlock() mu.RUnlock()
@ -121,20 +121,23 @@ func copyMap(m map[string]*Control) map[string]*Control {
return ret return ret
} }
func Init() { func init() {
err := os.MkdirAll("data/control", 0755) err := os.MkdirAll("data/control", 0755)
if err != nil { if err != nil {
panic(err) panic(err)
} }
}
func Init() {
zero.OnCommandGroup([]string{"启用", "enable"}, zero.AdminPermission, zero.OnlyGroup). zero.OnCommandGroup([]string{"启用", "enable"}, zero.AdminPermission, zero.OnlyGroup).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
model := extension.CommandModel{} model := extension.CommandModel{}
_ = ctx.Parse(&model) _ = ctx.Parse(&model)
service, ok := Lookup(model.Args) service, ok := lookup(model.Args)
if !ok { if !ok {
ctx.Send("没有找到指定服务!") ctx.Send("没有找到指定服务!")
} }
service.Enable(ctx.Event.GroupID) service.enable(ctx.Event.GroupID)
ctx.Send(message.Text("已启用服务: " + model.Args)) ctx.Send(message.Text("已启用服务: " + model.Args))
}) })
@ -142,11 +145,11 @@ func Init() {
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
model := extension.CommandModel{} model := extension.CommandModel{}
_ = ctx.Parse(&model) _ = ctx.Parse(&model)
service, ok := Lookup(model.Args) service, ok := lookup(model.Args)
if !ok { if !ok {
ctx.Send("没有找到指定服务!") ctx.Send("没有找到指定服务!")
} }
service.Disable(ctx.Event.GroupID) service.disable(ctx.Event.GroupID)
ctx.Send(message.Text("已关闭服务: " + model.Args)) ctx.Send(message.Text("已关闭服务: " + model.Args))
}) })
@ -154,7 +157,7 @@ func Init() {
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
model := extension.CommandModel{} model := extension.CommandModel{}
_ = ctx.Parse(&model) _ = ctx.Parse(&model)
service, ok := Lookup(model.Args) service, ok := lookup(model.Args)
if !ok { if !ok {
ctx.Send("没有找到指定服务!") ctx.Send("没有找到指定服务!")
} }
@ -169,7 +172,7 @@ func Init() {
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
msg := `---服务列表---` msg := `---服务列表---`
i := 0 i := 0
ForEach(func(key string, manager *Control) bool { forEach(func(key string, manager *Control) bool {
i++ i++
msg += "\n" + strconv.Itoa(i) + `: ` + key msg += "\n" + strconv.Itoa(i) + `: ` + key
return true return true