55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"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 {
|
|
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
|
|
}
|