mirror of
https://github.com/laoxong/nofx.git
synced 2026-06-04 09:58:22 +08:00
feat(hook): Add hook module to help decouple some specific logic (#784)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package hook
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
type HookFunc func(args ...any) any
|
||||
|
||||
var (
|
||||
Hooks map[string]HookFunc = map[string]HookFunc{}
|
||||
EnableHooks = true
|
||||
)
|
||||
|
||||
func HookExec[T any](key string, args ...any) *T {
|
||||
if !EnableHooks {
|
||||
log.Printf("🔌 Hooks are disabled, skip hook: %s", key)
|
||||
var zero *T
|
||||
return zero
|
||||
}
|
||||
if hook, exists := Hooks[key]; exists && hook != nil {
|
||||
log.Printf("🔌 Execute hook: %s", key)
|
||||
res := hook(args...)
|
||||
return res.(*T)
|
||||
} else {
|
||||
log.Printf("🔌 Do not find hook: %s", key)
|
||||
}
|
||||
var zero *T
|
||||
return zero
|
||||
}
|
||||
|
||||
func RegisterHook(key string, hook HookFunc) {
|
||||
Hooks[key] = hook
|
||||
}
|
||||
|
||||
// hook list
|
||||
const (
|
||||
GETIP = "GETIP" // func (userID string) *IpResult
|
||||
NEW_BINANCE_TRADER = "NEW_BINANCE_TRADER" // func (userID string, client *futures.Client) *NewBinanceTraderResult
|
||||
NEW_ASTER_TRADER = "NEW_ASTER_TRADER" // func (userID string, client *http.Client) *NewAsterTraderResult
|
||||
SET_HTTP_CLIENT = "SET_HTTP_CLIENT" // func (client *http.Client) *SetHttpClientResult
|
||||
)
|
||||
Reference in New Issue
Block a user