optz.
This commit is contained in:
@@ -5,18 +5,29 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
ftsService "bsm/full/module/base/fts/service"
|
||||
mgtService "bsm/full/module/base/mgt/service"
|
||||
passportService "bsm/full/module/base/passport/service"
|
||||
senderService "bsm/full/module/base/sender/service"
|
||||
walletService "bsm/full/module/finance/wallet/service"
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
)
|
||||
|
||||
const ServiceKey = "all"
|
||||
|
||||
type ServiceConfig map[string]any
|
||||
|
||||
type SrvConfig struct {
|
||||
conf.Base `yaml:",inline"`
|
||||
Databases *conf.DBConf `yaml:"Databases"`
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"`
|
||||
Services map[string]ServiceConfig `yaml:"Services"`
|
||||
conf.Base `yaml:",inline"`
|
||||
Databases *conf.DBConf `yaml:"Databases"`
|
||||
MicroService *conf.MicroServiceConf `yaml:"MicroService"`
|
||||
Rpc map[string]conf.RpcConf `yaml:"Rpc"`
|
||||
Apm *conf.ApmConf `yaml:"APM"`
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"`
|
||||
Services []string `yaml:"Services"`
|
||||
Fts *ftsService.Config `yaml:"Fts"`
|
||||
Mgt *mgtService.Config `yaml:"Mgt"`
|
||||
Passport *passportService.Config `yaml:"Passport"`
|
||||
Sender *senderService.Config `yaml:"Sender"`
|
||||
Wallet *walletService.Config `yaml:"Wallet"`
|
||||
}
|
||||
|
||||
var Spec SrvConfig
|
||||
@@ -27,9 +38,28 @@ func New() {
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
assignSharedConfig()
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
|
||||
func assignSharedConfig() {
|
||||
if Spec.Fts != nil {
|
||||
Spec.Fts.Base, Spec.Fts.Databases, Spec.Fts.Rpc, Spec.Fts.Apm, Spec.Fts.Etcd = Spec.Base, Spec.Databases, Spec.Rpc, Spec.Apm, Spec.Etcd
|
||||
}
|
||||
if Spec.Mgt != nil {
|
||||
Spec.Mgt.Base, Spec.Mgt.Databases, Spec.Mgt.Rpc, Spec.Mgt.Apm, Spec.Mgt.Etcd = Spec.Base, Spec.Databases, Spec.Rpc, Spec.Apm, Spec.Etcd
|
||||
}
|
||||
if Spec.Passport != nil {
|
||||
Spec.Passport.Base, Spec.Passport.Databases, Spec.Passport.MicroService, Spec.Passport.Rpc, Spec.Passport.Apm, Spec.Passport.Etcd = Spec.Base, Spec.Databases, Spec.MicroService, Spec.Rpc, Spec.Apm, Spec.Etcd
|
||||
}
|
||||
if Spec.Sender != nil {
|
||||
Spec.Sender.Base, Spec.Sender.Databases, Spec.Sender.MicroService, Spec.Sender.Rpc, Spec.Sender.Apm, Spec.Sender.Etcd = Spec.Base, Spec.Databases, Spec.MicroService, Spec.Rpc, Spec.Apm, Spec.Etcd
|
||||
}
|
||||
if Spec.Wallet != nil {
|
||||
Spec.Wallet.Base, Spec.Wallet.Databases, Spec.Wallet.MicroService, Spec.Wallet.Rpc, Spec.Wallet.Apm, Spec.Wallet.Etcd = Spec.Base, Spec.Databases, Spec.MicroService, Spec.Rpc, Spec.Apm, Spec.Etcd
|
||||
}
|
||||
}
|
||||
|
||||
func Enabled(name string) bool {
|
||||
if raw := strings.TrimSpace(os.Getenv("BSM_SERVICES")); raw != "" {
|
||||
for _, selected := range strings.Split(raw, ",") {
|
||||
@@ -39,10 +69,10 @@ func Enabled(name string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
service, ok := Spec.Services[name]
|
||||
if !ok {
|
||||
return false
|
||||
for _, service := range Spec.Services {
|
||||
if strings.EqualFold(strings.TrimSpace(service), name) || strings.EqualFold(strings.TrimSpace(service), "all") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
enabled, ok := service["Enable"].(bool)
|
||||
return !ok || enabled
|
||||
return false
|
||||
}
|
||||
|
||||
26
all/internal/config/config_test.go
Normal file
26
all/internal/config/config_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"go.yaml.in/yaml/v3"
|
||||
)
|
||||
|
||||
func TestAllDevConfig(t *testing.T) {
|
||||
data, err := os.ReadFile(filepath.Join("..", "..", "etc", "all_dev.yaml"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var cfg SrvConfig
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(cfg.Services) == 0 {
|
||||
t.Fatal("services must not be empty")
|
||||
}
|
||||
if cfg.Fts == nil || cfg.Mgt == nil || cfg.Passport == nil || cfg.Sender == nil || cfg.Wallet == nil {
|
||||
t.Fatal("service-specific configuration is incomplete")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user