Skip to content

Commit

Permalink
feat(control): add AutoRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Sep 3, 2023
1 parent adc600c commit 7f635c2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions control/register.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package control

import (
"runtime"
"strings"
"sync/atomic"

ctrl "github.com/FloatTech/zbpctrl"
Expand All @@ -23,6 +25,21 @@ func LoadCustomPriority(m map[string]uint64) {
prio = uint64(len(custpriomap)+1) * 10
}

// AutoRegister 根据包名自动注册插件
func AutoRegister(o *ctrl.Options[*zero.Ctx]) *Engine {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("unable to get caller")
}
name := runtime.FuncForPC(pc).Name()
a := strings.LastIndex(name, "/")
b := strings.LastIndex(name, ".")
if a < 0 || b < 0 || a >= b {
panic("invalid package name: " + name)
}
return Register(name[a:b], o)
}

// Register 注册插件控制器
func Register(service string, o *ctrl.Options[*zero.Ctx]) *Engine {
if custpriomap != nil {
Expand Down

0 comments on commit 7f635c2

Please sign in to comment.