refactor: add unified all service gateway
This commit is contained in:
48
all/internal/config/config.go
Normal file
48
all/internal/config/config.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"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"`
|
||||
}
|
||||
|
||||
var Spec SrvConfig
|
||||
|
||||
func New() {
|
||||
conf.New(ServiceKey, &Spec)
|
||||
Spec.Port = conf.CheckPort(Spec.Port)
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
|
||||
func Enabled(name string) bool {
|
||||
if raw := strings.TrimSpace(os.Getenv("BSM_SERVICES")); raw != "" {
|
||||
for _, selected := range strings.Split(raw, ",") {
|
||||
if strings.EqualFold(strings.TrimSpace(selected), name) || strings.EqualFold(strings.TrimSpace(selected), "all") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
service, ok := Spec.Services[name]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
enabled, ok := service["Enable"].(bool)
|
||||
return !ok || enabled
|
||||
}
|
||||
Reference in New Issue
Block a user