Skip to content

Commit

Permalink
✏️ 修复首次启动可能 panic
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Sep 10, 2021
1 parent b02789b commit 308ec05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion control/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (

func Register(service string, o *Options) *zero.Engine {
engine := zero.New()
engine.UsePreHandler(New(service, o).Handler())
engine.UsePreHandler(new(service, o).Handler())
return engine
}
41 changes: 22 additions & 19 deletions control/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type Control struct {
options Options
}

// New returns Manager with settings.
func New(service string, o *Options) *Control {
// new returns Manager with settings.
func new(service string, o *Options) *Control {
m := &Control{service: service,
options: func() Options {
if o == nil {
Expand All @@ -48,8 +48,8 @@ func New(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 @@ -58,8 +58,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 @@ -84,25 +84,25 @@ func (m *Control) Handler() zero.Rule {
}
m.RUnlock()
if m.options.DisableOnDefault {
m.Disable(ctx.Event.GroupID)
m.disable(ctx.Event.GroupID)
} else {
m.Enable(ctx.Event.GroupID)
m.enable(ctx.Event.GroupID)
}
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.
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 All @@ -121,40 +121,43 @@ func copyMap(m map[string]*Control) map[string]*Control {
return ret
}

func Init() {
func init() {
err := os.MkdirAll("data/control", 0755)
if err != nil {
panic(err)
}
}

func Init() {
zero.OnCommandGroup([]string{"启用", "enable"}, 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.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 @@ -169,7 +172,7 @@ 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) + `: ` + key
return true
Expand Down

0 comments on commit 308ec05

Please sign in to comment.