refactor: centralize protobuf and remove go-zero
This commit is contained in:
@@ -1,14 +1,54 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/engine/types"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Config retains the legacy delivery YAML shape so existing deployments continue to work.
|
||||
// Cache and Pulsar are deprecated and intentionally ignored because the service never used them.
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DSN string `json:",optional"`
|
||||
Cache string `json:",optional"`
|
||||
Anonymous types.AnonymousConf `json:",optional"`
|
||||
Pulsar types.PulsarConf `json:",optional"`
|
||||
Name string `yaml:"Name"`
|
||||
ListenOn string `yaml:"ListenOn"`
|
||||
DSN string `yaml:"Dsn"`
|
||||
Cache string `yaml:"Cache"`
|
||||
Anonymous types.AnonymousConf `yaml:"Anonymous"`
|
||||
Pulsar types.PulsarConf `yaml:"Pulsar"`
|
||||
}
|
||||
|
||||
func Load(serviceKey string) (Config, error) {
|
||||
mode := strings.ToLower(envOr("BlocksMesh_RuntimeMode", "dev"))
|
||||
prefix := envOr("BlocksMesh_Prefix", ".")
|
||||
workspace := envOr("BlocksMesh_Workspace", "def")
|
||||
path := filepath.Join(prefix, "etc", fmt.Sprintf("%s_%s.yaml", serviceKey, mode))
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("read config %s: %w", path, err)
|
||||
}
|
||||
replacer := strings.NewReplacer(
|
||||
"{ServiceKey}", serviceKey,
|
||||
"{Workspace}", workspace,
|
||||
"{RuntimeMode}", mode,
|
||||
)
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal([]byte(os.ExpandEnv(replacer.Replace(string(data)))), &cfg); err != nil {
|
||||
return Config{}, fmt.Errorf("parse config %s: %w", path, err)
|
||||
}
|
||||
if cfg.ListenOn == "" {
|
||||
return Config{}, fmt.Errorf("ListenOn is required in %s", path)
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func envOr(key, fallback string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user